我有以下两个课程
class Car < Vehicle
has_one :steering_wheel, as: :attached
end
class SteeringWheel < ActiveRecord::Base
belongs_to :attached
has_many :components
has_one :rim, class_name: 'Components', order: 'id DESC'
attr_accessible :components
end
class Component < ActiveRecord::Base
include SpecificationFileService::Client
attr_accessible :created_by
belongs_to :steering_wheel
end
然后在我的规格中:
context "given an attachment", :js do
before do
@car = create(:car, make: "honda")
@steering_wheel = SteeringWheel.create(attached: @car)
@steering_wheel.save
@car.save
@car.reload
end
specify "test the setup", :js do
puts @car.steering_wheel
end
end
哪个打印:nil
我发现解决此问题的一种方法是在汽车上显式设置转向轮,如下所示:
@car.steering_wheel = @steering_wheel
就在save
.
编辑:
正如下面评论中所建议的,我尝试添加 polymorphic: true,但这并没有解决问题。此外,我还充实了上面的 SteeringWheel 模型
我的问题是为什么,以及如何将其隐式添加到回调链中