【问题标题】:How to add "and !=" in ActiveRecord query如何在 ActiveRecord 查询中添加“和!=”
【发布时间】:2016-02-18 11:51:23
【问题描述】:

我需要在此查询中添加一个带有 AND!= 的条件 (即:和 properties.property_type_id != 'NULL')

如何在这个活动记录中添加?

@properties = Property.where(category_id: Admin
  .select("category_id").where(id: current_user.id))

请帮忙 任何帮助都是值得赞赏的

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 activerecord


    【解决方案1】:

    试试这个

    @properties = Property.where("category_id IN (?) AND property_type_id IS NOT NULL", Admin.where(id: current_user.id).map(&:category_id) )
    

    我希望这会有所帮助

    【讨论】:

    • 乐于助人...您不妨选择正确答案。 :)
    【解决方案2】:

    Hello 'and' 查询正在向查询添加 where 条件。

    @properties = Property.where(category_id: Admin.select("category_id").where(id: current_user.id)).where('property_type_id is NOT NULL')

    【讨论】:

    • hai,请也试试这个,告诉我它是否有效?据我所知。
    【解决方案3】:

    您最初需要映射所有 category_ids

    category_ids = Admin.where(:id => current_user.id).map(&:category_id)
    

    这将返回您需要的所有 category_id。

    现在您从属性表中获取所需的结果。

    @properties = Property.where("category_id IN (?) AND property_type_id IS NOT NULL",category_ids)
    

    【讨论】:

    • 这也有效 :),感谢所有帮助我的人 :)
    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 2013-10-06
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 2020-01-01
    相关资源
    最近更新 更多