0
votes

What is the problem with this association?

My association looks like this:

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

When i run

q=Quote.find(1)
q.language_pairs.create!(source_language:'French - EU', w_flow_id: 1)

I have following errors:

ActiveRecord::HasManyThroughCantAssociateThroughHasOneOrManyReflection: Cannot modify association 'LanguagePair#w_flow_steps' because the source reflection class 'WFlowStep' is associated to 'WFlow' via :has_many.

1
What do you want to do? create a language_pairs associated with theQuote with the id = 1?Alejandro Montilla
That exception class name tho..Ruslan

1 Answers

0
votes

If you want to create a LanguagePair a related with a particular Quote, first you need to get the Quote that you want to be associated:

q = Quote.find(1)

Then you pass that variable to the create method of your LanguagePair:

lp = LanguagePair.create!(quote: q, source_language:'French - EU', w_flow_id: 1)

And Rails will take care about the relations.

Note: you are using create! that will raise an exception if the record is invalid