Rails 多个 has_one 关联

发布于 2024-09-26 00:30:30 字数 581 浏览 5 评论 0原文

我有多个带有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难得心□动 2024-10-03 00:30:30

您必须添加的第一件事是可访问属性的规范。
在用户中,您必须添加:

attr_accessible :email, :created_by, :modified_by

在交易中:

attr_accessible :created_by, :modified_by

但您还应该更改关系的方向。 foreign_key总是在belongs_to一侧。

这对我有用:

class Deal < ActiveRecord::Base
  belongs_to  :created_by, :class_name => "User", :foreign_key => "created_by"
  belongs_to  :modified_by, :class_name => "User", :foreign_key =>"modified_by"

  attr_accessible :created_by, :modified_by, :name
end

class User < ActiveRecord::Base
  has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
  has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"

  attr_accessible :created_deals, :modified_deals, :name
end

如果您有更多看起来相似的模型,您可能可以使用多态关联: 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:

attr_accessible :email, :created_by, :modified_by

In Deal:

attr_accessible :created_by, :modified_by

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:

class Deal < ActiveRecord::Base
  belongs_to  :created_by, :class_name => "User", :foreign_key => "created_by"
  belongs_to  :modified_by, :class_name => "User", :foreign_key =>"modified_by"

  attr_accessible :created_by, :modified_by, :name
end

class User < ActiveRecord::Base
  has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
  has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"

  attr_accessible :created_deals, :modified_deals, :name
end

If you have more models, which look similiar you could probably use polymorphic associations: http://guides.rubyonrails.org/association_basics.html#polymorphic-associations

温折酒 2024-10-03 00:30:30

首先,根据我的经验,使用外键作为名称进行关联通常是一个坏主意。特别是在编写装置时,Rails 似乎会在设置实际值“created_by”或created_by 关联中的模型之间感到困惑。在我的模型中,我通常将这些关联用于您描述的情况:

belongs_to :creator, :class_name => "User", :foreign_key => 'created_by'
belongs_to :modifier, :class_name => "User", :foreign_key => 'modified_by'

如果您愿意,可以使用“creating_user”等关联名称。如果您确实希望created_by作为关联名称,则应该使用created_by_id或类似外键的内容,只要它不等于关联名称即可。

然后我对你粘贴的代码有点困惑。您选择“交易 has_one User”和“用户属于交易”意味着用户表将具有包含交易 ID 的 Created_by 和 Modified_by 列(外键),基本上意味着用户是通过单个交易创建的?然而,交易似乎应该由用户创建,而不是相反。您的 deal.created_by.email 示例根本无法与您的关联一起使用,因为交易不会有一个名为“created_by”的关联,只有“user”,其中您在单个模型中有两个同名的关联,这可以首先根本不起作用。

类似于帕特里克建议的那样修复你的关联:

class Deal < ActiveRecord::Base
  belongs_to  :creator, :class_name => "User", :foreign_key => "created_by"
  belongs_to  :modifier, :class_name => "User", :foreign_key =>"modified_by"
end

class User < ActiveRecord::Base
  has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
  has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
end

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:

belongs_to :creator, :class_name => "User", :foreign_key => 'created_by'
belongs_to :modifier, :class_name => "User", :foreign_key => 'modified_by'

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:

class Deal < ActiveRecord::Base
  belongs_to  :creator, :class_name => "User", :foreign_key => "created_by"
  belongs_to  :modifier, :class_name => "User", :foreign_key =>"modified_by"
end

class User < ActiveRecord::Base
  has_many :created_deals, :class_name => "Deal", :foreign_key => "created_by"
  has_many :modified_deals, :class_name => "Deal", :foreign_key => "modified_by"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文