2010-03-30 51 views
0

Rails中的has_many设置一个条件:我需要在需要的self.id变量的has_many来设定条件Rails的:我需要在需要的self.id变量

我需要的是这样的: 的has_many:帖子:条件=> “posts.hello_id =#{} self.id” 但self.id有它只是不工作:■

回答

0

这应该工作

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

class Foo 
    belongs_to :bar 
end 

基本上,你是提供SQL作为条件。

另一种选择,但我从来没有尝试过自己,可能是使用:finder_sql

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

我需要的是这样的: 的has_many:帖子:条件=>“posts.hello_id =#{自我.id}“ 但self.id那里它只是没有工作:s – 2010-03-31 00:06:53

+0

发布您的对象模型将在这里帮助。这个语句在Hello类中吗?如果是这样,那么假设Posts类具有belongs_to:hello语句,则不需要该条件。 – 2010-03-31 00:18:53