通过急切加载选择特定列

时间:2010-11-15 06:04:20

标签: ruby-on-rails activerecord

我正在使用Rails 2.3.5并试图找出在某些列上选择加载时的语法。例如,

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

让我们说在OrganizersController show方法中,我希望使用eager loading来从Organizer,Event和Booking模型中获取某些列。我相信这里是抓取所有3个模型中所有列的代码

Organizer.find(params[:id], :include => {:event => {:booking}})

但是我要说我只想抓住organizer.id,event.id和booking.id,这样做的语法是什么?

1 个答案:

答案 0 :(得分:0)

article中找到答案。

使用:仅限于此,例如,

david.to_json(:include => { :posts => { 
                            :include => { :comments => { 
                                          :only => :body } }, 
                            :only => :title } })