select * from users_messages t1 left join messages t2 on t1.message_id = t2.id where t1.id=20
class UsersMessages has_many :messages end
UsersMessages.find(20).messages
大概是这样吧。
joins("left join messages on users_messages.message_id = messages.id").where("users_messages.id=20")
貌似你应该把这 2 个表关联起来,关联之后就可以 joins(:message) 了
ActiveRecord::Base.connection.select("select * from users_messages t1 left join messages t2 on t1.message_id = t2.id where t1.id=20")
这个时候就显示出 Sequel 的可贵之处了
irb> DB[:user_messages_t1].left_join(:messages_t2).where(:"t1.id" => 20).sql
"SELECT * FROM user_messages As t1 LEFT JOIN messages AS t2 WHERE (t1.id = 20)"