开发者

Rails: I need to set a condition in a has_many that needs the self.id variable

Rails: I need to set a condition in a has_many that needs the self.id variable

I need something like this开发者_如何学编程: has_many :posts, :conditions => "posts.hello_id = #{self.id}" but self.id there it just doesnt work :s


This should work

class Bar
  has_many :foo, :conditions => "bar_id is NULL"
end

class Foo
  belongs_to :bar
end

Basically, you are providing SQL as the condition.

Another option, although I have never tried it myself, may be to use :finder_sql

has_many :posts, :finder_sql => 'select * from posts where hello_id = #{primary_key}'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜