Rails 多个 has_one 关联
我有多个带有created_by 和modified_by 列的模型。这就是我的交易模型。
class Deal
has_one :user , :foreign_key => 'created_by'
has_one :user , :foreign_key => 'modified_by'
end
class User
belongs_to :created_by , :class_name => 'Deal' , :foreign_key => 'created_by'
belongs_to :modified_by , :class_name => 'Deal' , :foreign_key => 'modified_by'
end
当我创建交易时,看起来它正在正确保存。但是在显示视图中,当我尝试获取 @deal.created_by.email
时,我收到“未定义方法 email
”错误。有人可以告诉我如何让它工作吗?
另外,由于我有多个具有这两列的模型,因此用户模型中可以有很多belongs_to。对于这种情况有一个优雅的解决方案吗?
I have multiple models with created_by and modified_by columns. This is what I have for a Deal Model.
class Deal
has_one :user , :foreign_key => 'created_by'
has_one :user , :foreign_key => 'modified_by'
end
class User
belongs_to :created_by , :class_name => 'Deal' , :foreign_key => 'created_by'
belongs_to :modified_by , :class_name => 'Deal' , :foreign_key => 'modified_by'
end
When I create the deal, looks like it is saving correctly. But in the show view when I try to get @deal.created_by.email
I get an "undefined method email
" error. Can some tell me how to get this working please?
Also since I have multiple models with these two columns, there can be many belongs_to in User model. Is there an elegant solution for this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须添加的第一件事是可访问属性的规范。
在用户中,您必须添加:
在交易中:
但您还应该更改关系的方向。 foreign_key总是在belongs_to一侧。
这对我有用:
如果您有更多看起来相似的模型,您可能可以使用多态关联: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
First thing you have to add is the specification of accessible attributes.
In User you would have to add:
In Deal:
But you should also change the direction of your relation. The foreign_key is always on the belongs_to side.
This is what worked for me:
If you have more models, which look similiar you could probably use polymorphic associations: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations
首先,根据我的经验,使用外键作为名称进行关联通常是一个坏主意。特别是在编写装置时,Rails 似乎会在设置实际值“created_by”或created_by 关联中的模型之间感到困惑。在我的模型中,我通常将这些关联用于您描述的情况:
如果您愿意,可以使用“creating_user”等关联名称。如果您确实希望created_by作为关联名称,则应该使用created_by_id或类似外键的内容,只要它不等于关联名称即可。
然后我对你粘贴的代码有点困惑。您选择“交易 has_one User”和“用户属于交易”意味着用户表将具有包含交易 ID 的 Created_by 和 Modified_by 列(外键),基本上意味着用户是通过单个交易创建的?然而,交易似乎应该由用户创建,而不是相反。您的 deal.created_by.email 示例根本无法与您的关联一起使用,因为交易不会有一个名为“created_by”的关联,只有“user”,其中您在单个模型中有两个同名的关联,这可以首先根本不起作用。
类似于帕特里克建议的那样修复你的关联:
First of all, from my experience it is generally a bad idea to have associations using the foreign key as name. Especially when writing fixtures it seems rails will get confused between setting the actual value "created_by" or the model in the created_by association. In my models I generally use these associations for the cases you describe:
You can use association names like 'creating_user' instead if you prefer. If you really want created_by as association name you should have created_by_id or something similar as foreign key, just as long as its not equal to the association name.
Then I am a bit confused by your pasted code. Your choice "Deal has_one User" and "User belongs_to Deal" means that the users table will have the columns created_by and modified_by (foreign keys) containing Deal Ids, basically meaning that users get created by a single deal? However it seems like deals should get created by users and not the other way round. Your example of deal.created_by.email can not work at all with your associations, since deal would not have an association called "created_by", only "user", of which you have two associations with the same name in a single model which can not work at all in the first place.
Fixing your associations similar to what Patrick suggested: