class Category < ActiveRecord::Base
has_many :events
end
class Event < ActiveRecord::Base
belongs_to :category
# ...
end
先用 joins Event.joins(:category) 执行的语句其实是
SELECT "events".* FROM "events" INNER JOIN "categories" ON "categories"."id" = "events"."category_id"
于是我用 includes @messages = Event.includes(:category).page(params[:page]).per(5)
返回的 messages 明显不是 category 的字段,因为我用。这样的属性都无法访问到。它其实是拆成了两条语句执行
SELECT * FROM events
SELECT * FROM categories WHERE categories.id IN (1,2,3...)
@messages得到的还是 SELECT * FROM events 这条语句的结构
而且,includes 时,# SELECT * FROM categories WHERE categories.id IN (1,2,3...)
,这种语句只是单条执行,达不到批量返回给@messages的效果
求大侠赐教,我想批量返回主键表中的内容。。