Answer the question
In order to leave comments, you need to log in
How to get a join table field?
There are two models - Member (participant) and Community (community).
Members can be in multiple communities. The migration and model data looks like this:
#models/member.rb
class Member < ActiveRecord::Base
has_and_belongs_to_many :communities
end
#models/community.rb
class Community < ActiveRecord::Base
has_and_belongs_to_many :members
end
#миграция
create_table :communities_members, id: false do |t|
t.belongs_to :member, index: true
t.belongs_to :community, index: true
t.timestamps null: false #это важная строчка
end
Answer the question
In order to leave comments, you need to log in
Just for such purposes, it is recommended to use has_many through instead of has_and_belongs_to_many:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question