0

我想在 Rails/Active Record 中写这个查询

SELECT *  FROM messages WHERE (realtor_id,created_at) IN  ( SELECT realtor_id, MAX(created_at),message   FROM messages   WHERE `messages`.`user_id` = 8   GROUP BY realtor_id );

我尝试了一些语法,但它不起作用......我认为,解决方案很接近:

@discussions = Message.where(realtor_id: created_at: [Message.where(:user_id => current_user.id).select("realtor_id, MAX(created_at) as created_at").group("realtor_id").order("created_at ASC")])

有人可以帮助我吗?

感谢提前,

F。

4

1 回答 1

1

您可以创建sql查询并传入

query = "SELECT *  FROM messages WHERE (realtor_id,created_at) IN  ( SELECT realtor_id, MAX(created_at),message FROM messages WHERE `messages`.`user_id` = 8   GROUP BY realtor_id )"

@discussions = ActiveRecord::Base.connection.execute(query)
于 2013-10-29T16:30:42.143 回答