Selecting specific columns with eager loading
I'm on Rails 2.3.5 and trying to figure out the syntax for selecting on certain columns with eager loading. For example,
class Organizer < ActiveRecord::Base
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :organizer
has_many :bookings
end
class Booking < ActiveRecord::Base
belongs_to :event
end
Let's say in OrganizersController show method, I wish to use eager loading to grab certain columns from Organizer, Event, and Booking models. I believe here's the code that grabs all the columns from all 3 models
Organizer.find(params[:id], :include => {:event => {:booking}})
开发者_StackOverflow中文版
But let's say that I only want to grab organizer.id, event.id, and booking.id, what's the syntax for doing so?
Found the answer in this article.
Use :only like this, e.g.,
david.to_json(:include => { :posts => {
:include => { :comments => {
:only => :body } },
:only => :title } })
精彩评论