I am new to rails. I have two models users and languages and i have list of languages in language model.
class User < ActiveRecord::Base
has_and_belongs_to_many :languages
and
class Language < ActiveRecord::Base
has_and_belongs_to_many :users
I have two questions :
- how can i add an existing language to a user’s language list ?
- how can i include the languages into the user’s json representation ?
I do not have any form i use curl to post and receive json as response as shown below :
curl -H ‘Content-Type: application/json’ -H ‘Accept:
application/json’ -X GET htt://localhost:3000/profile.json -d “{
“auth_token” : “teafa52qsasTDGaahawa” }”
Please help me. Thanks in advance.
use
@user.languages << @languages_to_addto create the records needed in the join table when updating your record.use
@user.to_json( include: :languages )in your controller to include the languages in your JSON response.