Ruby on Rails 3 howto make 'OR' condition

前端 未结 9 1345
無奈伤痛
無奈伤痛 2020-12-03 00:34

I need an SQL statement that check if one condition is satisfied:

SELECT * FROM my_table WHERE my_table.x=1 OR my_table.y=1

I want to do th

相关标签:
9条回答
  • 2020-12-03 01:20

    With rails_or, you could do it like:

    Account.where(id: 1).or(id: 2)
    

    (It works in Rails 4 and 5, too.)

    0 讨论(0)
  • 2020-12-03 01:24

    Sadly, the .or isn't implemented yet (but when it is, it'll be AWESOME).

    So you'll have to do something like:

    class Project < ActiveRecord::Base
      scope :sufficient_data, :conditions=>['ratio_story_completion != 0 OR ratio_differential != 0']
      scope :profitable, :conditions=>['profit > 0']
    

    That way you can still be awesome and do:

    Project.sufficient_data.profitable
    
    0 讨论(0)
  • 2020-12-03 01:24

    Those arel queries are unreadable to me.

    What's wrong with a SQL string? In fact, the Rails guides exposes this way as the first way to make conditions in queries: http://guides.rubyonrails.org/active_record_querying.html#array-conditions

    So, I bet for this way to do it as the "Rails way":

    Account.where("id = 1 OR id = 2")
    

    In my humble opinion, it's shorter and clearer.

    0 讨论(0)
提交回复
热议问题