如何让属性的setter通过SQL函数发送值 [英] How to make attribute setter send value through SQL function

查看:157
本文介绍了如何让属性的setter通过SQL函数发送值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在一个ActiveRecord模型的属性二传手包裹在text2ltree其价值()的Postgres函数生​​成的SQL查询。

I'm trying to make an attribute setter in an ActiveRecord model wrap its value in the text2ltree() postgres function before rails generates its sql query.

例如,

post.path = "1.2.3"
post.save

应该产生像

UPDATE posts SET PATH=text2ltree('1.2.3') WHERE id = 123 # or whatever

什么是这样做的最好方法是什么?

What's the best way of doing this?

推荐答案

编辑::要实现正是你所寻找的东西上面,你会用它来替代默认的setter模型中的文件

To achieve exactly what you are looking for above, you'd use this to override the default setter in your model file:

def path=(value)
  self[:path] = connection.execute("SELECT text2ltree('#{value}');")[0][0]
end

那么code你有上述作品。

Then the code you have above works.

我有兴趣了解更多有关的ActiveRecord的内部和坚不可摧的元编程基础,所以作为一个练习,我试图完成你在下面您的意见描述了。下面是为我工作(这是所有在post.rb)的一个例子:

I'm interested in learning more about ActiveRecord's internals and its impenetrable metaprogramming underpinnings, so as an exercise I tried to accomplish what you described in your comments below. Here's an example that worked for me (this is all in post.rb):

module DatabaseTransformation
  extend ActiveSupport::Concern

  module ClassMethods
    def transformed_by_database(transformed_attributes = {})

      transformed_attributes.each do |attr_name, transformation|

        define_method("#{attr_name}=") do |argument|
          transformed_value = connection.execute("SELECT #{transformation}('#{argument}');")[0][0]
          write_attribute(attr_name, transformed_value)
        end
      end
    end
  end
end

class Post < ActiveRecord::Base
  attr_accessible :name, :path, :version
  include DatabaseTransformation
  transformed_by_database :name => "length" 

end

控制台输出:

Console output:

1.9.3p194 :001 > p = Post.new(:name => "foo")
   (0.3ms)  SELECT length('foo');
 => #<Post id: nil, name: 3, path: nil, version: nil, created_at: nil, updated_at: nil> 

在现实生活中我presume你想要包括模块中的ActiveRecord :: Base的,在某个文件先前在负载路径。你也不得不妥善处理要传递到数据库函数的参数的类型。最后,我了解到, connection.execute 是由每个数据库适配器实现的,所以你访问的结果可能是Postgres的不同(这个例子的SQLite3,在结果集的方式返回为散列数组和密钥与第一数据记录是0]

In real life I presume you'd want to include the module in ActiveRecord::Base, in a file somewhere earlier in the load path. You'd also have to properly handle the type of the argument you are passing to the database function. Finally, I learned that connection.execute is implemented by each database adapter, so the way you access the result might be different in Postgres (this example is SQLite3, where the result set is returned as an array of hashes and the key to the first data record is 0].

本博客文章是难以​​置信的帮助:

This blog post was incredibly helpful:

http://www.fakingfantastic.com/2010/09/20/concerning-yourself-with-active-support-concern/

因为是Rails的指南插件创作:

as was the Rails guide for plugin-authoring:

http://guides.rubyonrails.org/plugins.html

另外,对于它的价值,我觉得在Postgres里使用迁移创建一个查询重写规则我还是会做,但本作的一个很好的学习经验。希望它的工作原理,我可以停止思考如何去做。

Also, for what it's worth, I think in Postgres I'd still do this using a migration to create a query rewrite rule, but this made for a great learning experience. Hopefully it works and I can stop thinking about how to do it now.

这篇关于如何让属性的setter通过SQL函数发送值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆