[英]Rails has_one/belongs_to associations not working as expected
I'm in the process of teaching myself Rails and I'm stumped as to why an association isn't working correctly. 我正在自学Rails,并且对为什么关联无法正常工作感到困惑。 I think I'm missing something pretty basic but I can't snuff out exactly what.
我想我缺少了一些基本的知识,但我无法完全了解到底是什么。
I have two classes -- Builds and Equipment. 我有两节课-建筑和设备。 Builds are made of 2 pieces of equipment, deviously titled position_1 and position_2.
建筑物由2台设备组成,分别命名为position_1和position_2。 Here's what my definitions look like:
这是我的定义:
class Build < ActiveRecord::Base
has_one :position_1, :class_name => "Equipment"
has_one :position_2, :class_name => "Equipment"
attr_accessor :position_1, :position_2
end
and 和
class Equipment < ActiveRecord::Base
belongs_to :build, :foreign_key => :position_1
belongs_to :build, :foreign_key => :position_2
end
(Ignore for the moment that this could be handled by a relationship table to support any number of positions -- I'm basically trying to figure out how to have a class with two has_one relationships to another class.) (暂时不考虑可以由关系表来处理,以支持任意数量的位置-我基本上是想弄清楚如何使一个类与另一个类具有两个has_one关系。)
Now if I try and do something simple like this.... 现在,如果我尝试做这样简单的事情...。
position_1 = Equipment.find(params[:build][:position_1])
position_2 = Equipment.find(params[:build][:position_2])
@build = Build.new
@build.position_1 = position_1
@build.position_2 = position_2
logger.debug("THE BUILD IS #{@build.inspect}")
I will have successfully created a build object with the equipment objects correctly assigned to the position_1 parameter, but the position_1 and position_2 fields of the build parameter are left nil. 我将成功创建一个带有正确分配给position_1参数的设备对象的构建对象,但是build参数的position_1和position_2字段保留为零。
logger.debug("THE EQUIPMENT IS #{@build.position_1}")
> EQUIPMENT IS #<Equipment:0x007fa0581705c0>
logger.debug("THE BUILD IS #{@build.inspect}")
> THE BUILD IS #<Build id: nil, position_1_id: nil, position_2_id: nil, created_at: "2013-05-27 18:00:32", updated_at: "2013-05-27 18:00:32">
What am I getting wrong here? 我这是怎么了?
First, I would not include the associations as "attr_accessors" in the "parent" ActiveRecord. 首先,我不会在“父” ActiveRecord中将这些关联包括为“ attr_accessors”。 Rails creates the correct association field for Build for you.
Rails为您为Build创建正确的关联字段。
When you create instances of the positions, like an instance of Equipment, the correct way to assign them to a new Build would be: 当您创建职位实例(例如“设备”实例)时,将其分配给新内部版本的正确方法是:
position_1 = Equipment.find(params[:build][:position_1])
position_2 = Equipment.find(params[:build][:position_2])
@build = Build.new
@build.position_1 << position_1
@build.position_2 << position_2
Here's good guide on Rails associations . 这是有关Rails协会的好指南。
Figured it out -- I had misunderstood part of how Rails does associations -- what I really needed was... 弄清楚了-我误解了Rails如何进行关联-我真正需要的是...
class Equipment < ActiveRecord::Base
has_many :builds, :foreign_key => :position_1
has_many :builds, :foreign_key => :position_2
end
class Build < ActiveRecord::Base
belongs_to :position_1, :class_name => "Equipment", :foreign_key => "position_1_id"
belongs_to :position_2, :class_name => "Equipment", :foreign_key => "position_2_id"
end
With this in place everything works as I expected. 有了这个适当的位置,一切都会按我的预期进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.