Rails范围 - 如何连接3个表

时间:2013-03-01 21:49:36

标签: ruby-on-rails ruby ruby-on-rails-3

我有3张桌子:

schools{id, name, desc, adress}
reviews{id, content, rating, school_id, user_id} # user_id & schoold_id is foregin keys
users{id, name, city}

如何编写rails范围,连接所有3个表并获取列 schools.namereviews.contentreviews.ratingusers.name

我正在使用rails 3.2

1 个答案:

答案 0 :(得分:1)

scope :mashup, joins(:school, :user).select("content, rating, schools.name as sname, users.name as uname")

您可以了解更多here