Hi,
I’m using habtm to create a relationship between two models:
class Question < ActiveRecord::Base
has_and_belongs_to_many :exams
end
class Exam < ActiveRecord::Base
has_and_belongs_to_many :questions
end
At some point I’m doing this:
class Exam < ActiveRecord::Base
has_and_belongs_to_many :questions
def generate
(1…5).each do
questions.create(Question::SKELETON)
end
end
end
When I do that, a sql inserts a record inside questions_exams table.
But this table has another column called: ‘order’ where I need to put
some
data inside
How can I populate the ‘order’ column inside join table ?
questions_exams
Thanks.
On 24 March 2015 at 18:50, Gm [email protected] wrote:
end
end
Don’t use habtm, use has many through
Exam has_many questions_exams
has_many questions through questions_exams
But probably don’t call it questions_exams, think of a better name
In fact even when I could use habtm I usually use has many through, I
find it easier to understand. Also it is surprising how often the
requirement for a little something extra in the join table pops up out
of the requirements when least expected.
Colin
On 26 March 2015 at 20:38, Gm [email protected] wrote:
questions.create(Question::SKELETON)
end
end
Possibly something like
exam.questions_exams.create(order: something).create_question(…)
You can find detailed definitions of the methods that the associations
provide in section 4 of
http://guides.rubyonrails.org/association_basics.html
Colin
Hi Colin,
Thanks for the reply.
Even if I use has many through, how can I populate the order field
dinamically ?
When I do this:
def generate
(1…5).each do
questions.create(Question::SKELETON)
end
end
Thank you.