小编Fil*_*evi的帖子

Rails:加入多个条件

我有一个简单的模型

class Interest < ActiveRecord::Base
  has_and_belongs_to_many :user_profiles
end

class UserProfile < ActiveRecord::Base
  has_and_belongs_to_many :interests
end
Run Code Online (Sandbox Code Playgroud)

当我想查询具有特定兴趣的所有用户时,这很简单

UserProfile.joins(:interests).where('interests.id = ?', an_interest)
Run Code Online (Sandbox Code Playgroud)

但是,我如何找到有多个兴趣的用户?当然,如果我这样做

UserProfile.joins(:interests).where('interests.id = ?', an_interest).where('interests.id = ?', another_interest)
Run Code Online (Sandbox Code Playgroud)

我总是得到一个空结果,因为在连接之后,没有行可以同时拥有interest.id = an_interest和interest.id = another_interest.

ActiveRecord中有没有办法表达"我想要有2个(指定)兴趣的用户列表?

更新(解决方案)这是我提出的第一个工作版本,感谢Omar Qureshi

    specified_interests.each_with_index do |i, idx|
      main_join_clause = "interests_#{idx}.user_profile_id = user_profiles.id"
      join_clause = sanitize_sql_array ["inner join interests_user_profiles interests_#{idx} on
                    (#{main_join_clause} and interests_#{idx}.interest_id = ?)", i]

      relation = relation.joins(join_clause)
    end
Run Code Online (Sandbox Code Playgroud)

activerecord join ruby-on-rails-3

8
推荐指数
1
解决办法
1万
查看次数

Emacs:删除表达式之间的空格

在编写Clojure代码时,我经常在最后一个表达式和结束括号之间留下空格.就像是

(defn myfunction
  [arg]
  (do
    (expr1)
    (expr2)|
  ))
Run Code Online (Sandbox Code Playgroud)

哪里| 是光标的位置.Emacs中是否有一个快捷方式来删除(expr2)和最后一个括号之间的空格?目标是结束

(defn myfunction
  [arg]
  (do
    (expr1)
    (expr2)))
Run Code Online (Sandbox Code Playgroud)

emacs whitespace elisp clojure key-bindings

5
推荐指数
1
解决办法
287
查看次数