简体   繁体   中英

How to create active record query in rails?

I'm new in rails and I have a sql query but I don't know how can I convert it into rails active records query.

UserExercise.connection.exec_query("SELECT * FROM (SELECT \"user_courses_progresses\".\"user_id\", \"user_courses_progresses\".\"progress\" FROM \"user_courses_progresses\" WHERE \"user_courses_progresses\".\"is_primary\" IS TRUE) x JOIN users ON \"x\".\"user_id\" = \"users\".\"id\" ORDER BY progress desc")

Combining Active Record and "raw" SQL that might look like this:

User
  .select('*')
  .from(
    UserCoursesProgress
      .select(:user_id, :progress)
      .where(is_primary: true),
    :x
  )
  .joins('JOIN users ON x.user_id = users.id')
  .order(progress: :desc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM