这种关联有什么问题?
我的关联看起来像这样:
class Quote < ApplicationRecord
has_many :language_pairs
end
class LanguagePair < ApplicationRecord
belongs_to :quote
belongs_to :w_flow
has_many :w_flow_steps, through: :w_flow
end
class WFlow < ApplicationRecord
has_many :language_pairs
has_many :w_flow_steps
end
class WFlowStep < ApplicationRecord
belongs_to :w_flow
end
当我跑步时
q=Quote.find(1)
q.language_pairs.create!(source_language:'French - EU', w_flow_id: 1)
我有以下错误:
ActiveRecord :: HasManyThroughCantAssociateThroughHasOneOrManyReflection:无法修改关联&#39; LanguagePair#w_flow_steps&#39;因为源反射类&#39; WFlowStep&#39;与'WFlow&#39;相关联via:has_many。
答案 0 :(得分:0)
如果您想创建与特定LanguagePair
相关联的Quote
,首先需要获取您想要关联的Quote
:
q = Quote.find(1)
然后将该变量传递给LanguagePair
:
lp = LanguagePair.create!(quote: q, source_language:'French - EU', w_flow_id: 1)
Rails将关注这些关系。
注意:您正在使用create!
,如果记录无效,则会引发异常