[英]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. 我对Postgresql不熟悉,并且知道有一些区别。 How do I change this to query in postgres?
如何将其更改为在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)
Rails将负责在此数组中连接值
@user.courses.map(&:name)
You don't need to do it manually. 您不需要手动进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.