当使用activerecord从数据库中获取内容时,我想要在两个表中获取具有指定列的自定义结果集。Rails 2:连接和:包含结果集
SELECT users.name, users.username, users.age, users.city_id, cities.name as city_name FROM users INNER JOIN cities ON users.city_id = cities.id
这将是AR作为
Users.find(:all,
:joins => :cities,
:select => "users.name, users.username, users.age, users.city_id,
cities.name as city_name")
但这仅返回用户表的结果,而不是城市的结果。我100%确定内部联接语句正在经历(两个表都正在联接)。
看起来好像返回对象只有与模型相关的列。因此,UserModel只有users表具有的列,即使它们在select中指定,也不允许获取城市表的列。
我应该使用:joins还是:include?任何想法发生了什么?