3

I have a Rails application which displays nested form in json format.

In the JSON Response i am also displaying an id field which represent another table.

How to display name corresponding to that id what i am getting so that i can display both name and id in my json format.

My controller show method

def show
    @maintemplate = Maintemplate.find(params[:id])
    respond_with (@maintemplate) do |format|
      format.json { render :json => @maintemplate }
    end
  end

Thanks in advance....

7
  • I'm not sure I understand your question, but how about render :json => @maintemplate.to_json? Commented Nov 29, 2012 at 4:08
  • i am getting the Json format quite well,problem is ,in my json format i am displaying an id field(eg: "user_id" : "12" ),which corresponds to User table. I want to include the name corresponding to this id("12") in my json format.How to fix this. Commented Nov 29, 2012 at 4:35
  • Ok posted an answer, see below. Commented Nov 29, 2012 at 4:53
  • i dont want to change the name in json format,i want to fetch the name corresponding to this id(say id=12) from my user table ,so that i can display both id and name in my json format. Commented Nov 29, 2012 at 6:55
  • I think you're misunderstanding. The code below returns a JSON where ["user"]["name"] is the name of the user with user_id = 12. That user id will also be in the JSON. I've updated my example below to make this clear (but the code is the same). Commented Nov 29, 2012 at 7:00

2 Answers 2

4

Try this:

render :json => @maintemplate.to_json(:include => { :user => { :only => :name } } )

This will replace the user_id key with a user key and a value with only the name attribute of user, like this:

{
  "user_id": "12"
  "user": { "name": "..." }
  ...
}

You can then access the username in the json response with ["user"]["name"]. You can also access the user id with ["user_id"].

For more see the documentation on as_json.

Update:

Using the info provided in the comments, I think this is what you actually want:

render :json => @maintemplate.to_json(:include => { :routine => { :include => :user, :user => { :only => :name } } } )
Sign up to request clarification or add additional context in comments.

5 Comments

my "@maintemplte" is a nested form output .it has array of template1 inside "@maintemplate" , template1 is having array of template2....my user_id is lying inside each template2.
Can you please explain more clearly? What do you mean @maintempalte is a nested form output? It's the result of a find method call, so it should be a model instance.
"@maintemplate" is a model instance.It containd "days" array.Days array is having "routine" array.All these are comming from different model (days, routine, maintemplate)."maintemplate" model id having day_id and routine_id.Routine model is having user_id (ie displaying in json format as output with many other field).This user_id corresponds to user table which is having name ,age etc.... I want to include this name also in this json output corresponds to what user_id i am getting.
Can you clarify: does your Maintemplate model has_many :routines, or has_one :routine, or does it belong_to :routine? It's not clear from what you wrote above. When you describe your models, you should describe the associations (have_many, have_one, belongs_to etc.) Saying a model has an "array" is not really correct, it is an association not an array.
Posted an update, but I'm still not sure it will work. I think at this point you'd better try figuring this out yourself.
1

Add to as_json method with the additional method-attributes you desire to the class in which you are calling.

class MainTemplate
  ...

  def name
    User.find(self.user_id).name
  end

  def as_json(options = {})
    options[:methods] = :name
    super(options)
  end

end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.