0

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.

m_x
  • 12,357
  • 7
  • 46
  • 60
logesh
  • 2,572
  • 4
  • 33
  • 60
  • What do you have in your users controller? – Substantial Dec 01 '12 at 10:36
  • edited your question to make it more readable, i suggest you add details about your controller as @gg_s requested, and open two distinct questions in the future (on SO, always ask one question per problem to avoid confusion and make the answer more easy to retrieve for people who have the same problem as you) – m_x Dec 01 '12 at 18:25

1 Answers1

2

use @user.languages << @languages_to_add to 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.

m_x
  • 12,357
  • 7
  • 46
  • 60