0

i would like write this query in 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 );

I tried some syntaxes but it doesnt work... I think, the solution is near :

@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")])

Someone can help me ?

Thanks for advance,

F.

1 Answer 1

1

You can create sql query and pass in

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.