简体   繁体   中英

How would I translate this sqlite query into a postgresql query in Ruby on Rails?

I am trying to filter my posts and I grab them using this query.

@posts = Post.all.includes(:course).where("courses.name IN (#{@user.courses.map(&:name).collect { |s| "'#{s}'"  }.join(',') })").references(:course).order("posts.created_at DESC")

I am not familiar with postgresql and know there are some differences. How do I change this to query in postgres?

Your query will work as it is. But you can modify the query to avoid the unnecessary operations you are doing:

@posts = 
  Post.all.includes(:course).where("courses.name IN (?)", @user.courses.map(&:name)).order("posts.created_at DESC")

Rails will take care of joining the values in this array @user.courses.map(&:name)

You don't need to do it manually.

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