Reputation: 97
Here is the context : I have Users
and I want to store what languages
they speak.
How should I do this ? Create a Languages
table with association has_many
for Users
? Store it as a hstore in a languages User's column ?
Also, I'm using simple-form
, I would like to populate a select (:multiple
because a User
can speak multiple languages) with all the languages, any idea how to achieve that ? Should I create a constant with all the languages ? Maybe a gem that accomplishes that ?
I did my research but couldn't find anything that fits this need.
Upvotes: 0
Views: 41
Reputation: 20253
Looks like a basic M:M relation between User
and Language
(which is thoroughly described in the Active Record Associations guide).
How about a User
model like:
class User < ApplicationRecord
has_many :user_languages
has_many :languages, through: :user_languages
end
And a Language
model like:
class Language < ApplicationRecord
has_many :user_languages
has_many :users, through: :user_languages
end
And a UserLanguage
model like:
class UserLanguage < ApplicationRecord
belongs_to :user
belongs_to :language
end
I imagine you would seed your languages
table.
Upvotes: 1