Search code examples
ruby-on-railsactiverecord

Ruby active record for SQL


I need Ruby active record query for this SQL:

SELECT `properties`.* FROM `properties`
INNER JOIN `recentviews` ON `recentviews`.`property_id` = `properties`.`id`
WHERE `recentviews`.`user_id` = 2 ORDER BY `recentviews`.`view_time` DESC

Solution

  • Try this:

    Property.joins(:recentviews).where(recentviews: {user_id: 2}).order("recentviews.view_time DESC")