Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
211 views
in Technique[技术] by (71.8m points)

Include associated model when rendering JSON in Rails

Right now I have this line:

 render json: @programs, :except => [:created_at, :updated_at]

However, since a Program belongs_to a Company I would like to show the Company name instead of the Company Id.

How can I include the company name when rendering Programs?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this should work:

render :json => @programs, :include => {:insurer => {:only => :name}}, :except => [:created_at, :updated_at]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...