导轨如果指定的has_many制定者应该设置条件 [英] rails has_many setter should set conditions if specified
问题描述
这似乎是对我在Rails中的一个错误,但有可能是没有多少我能做到这一点。所以,我怎么能做到我期望的行为?
This seems like a bug in Rails to me, but there's probably not much I can do about that. So how can I accomplish my expected behavior?
假设我们有:
class User < ActiveRecord::Base
has_many :awesome_friends, :class_name => "Friend", :conditions => {:awesome => true}
end
和执行code:
>> my_user.awesome_friends << Friend.new(:name=>'jim')
后来,当我检查这个朋友的对象,我看到USER_ID字段填充。但我也希望看到的真棒列设置为真,而事实并非如此。
Afterwards, when I inspect this friend object, I see that the user_id field is populated. But I would also expect to see the "awesome" column set to 'true', which it is not.
此外,如果我执行从控制台执行以下操作:
Furthermore, if I execute the following from the console:
>> my_user.awesome_friends << Friend.new(:name=>'jim')
>> my_user.awesome_friends
= [#<Friend id:1, name:"jim", awesome:nil>]
# Quit and restart the console
>> my_user.awesome_friends
= []
在这个有什么想法?我假设条件散列可以任意复杂,使得融入二传手是不可能的。但在某种程度上,它感觉就像在默认情况下,我们是路过的条件?:user_ID的=> self.id,并获取设置,所以应该不是别人
Any thoughts on this? I suppose the conditions hash could be arbitrarily complex, making integration into the setter impossible. But in a way it feels like by default we are passing the condition ":user_id => self.id", and that gets set, so shouldn't others?
谢谢, 迈克
编辑:
我发现,有回调的has_many,所以我想我可能会这样定义的关系:
I found that there are callbacks for has_many, so I think I might define the relationship like this:
has_many :awesome_friends,
:class_name => "Friend",
:conditions => {:awesome => true},
:before_add => Proc.new{|p,c| c.awesome = true},
:before_remove => Proc.new{|p,c| c.awesome = false}
尽管,它开始觉得,也许我只是执行一些其他的,现有的设计模式。也许我应该继承AwesomeFriend&LT;朋友?最终,我需要一些这些的has_many关系,子类获得的凌乱与所有多余的文件。
Although, it's starting to feel like maybe I'm just implementing some other, existing design pattern. Maybe I should subclass AwesomeFriend < Friend? Ultimately I need a couple of these has_many relationships, and subclassing get's messy with all the extra files..
编辑2:
好了,感谢大家谁评论!我最终结束了上面的方法插入一个可爱的小ActiveRecord的扩展,has_many_of_type。其工作方式类似于如下:
Okay, thanks to everyone who commented! I ultimately wrapped up the method above into a nice little ActiveRecord extension, 'has_many_of_type'. Which works like follows:
has_many_of_type :awesome_friends, :class_name => "Friend", :type=>:awesome
刚刚转化为HAS_MANY用适当的条件下,before_add和before_remove PARAMS(并假设一个名为列friend_type的存在)。
Which just translates to has_many with the appropriate conditions, before_add, and before_remove params (and it assumes the existence of a column named friend_type).
推荐答案
您需要使用:
my_user.awesome_friends.create(:名称=&GT;'吉姆')
或 my_user.awesome_friends.build(:名称=&GT;吉姆')
在文档: 的has_many(:条件)
In documentation: has_many (:conditions)
记录从联想的创作范围。的has_many:帖子,:条件=> {:已发布=>真正}将创建发布的文章有@ blog.posts.create或@ blog.posts.build
Record creations from the association are scoped if a hash is used. has_many :posts, :conditions => {:published => true} will create published posts with @blog.posts.create or @blog.posts.build.
这篇关于导轨如果指定的has_many制定者应该设置条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!