Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Rails 4 Active Record query OR

Active Record OR where

  • Please log in to reply
4 replies to this topic

#1 reyntjensw

reyntjensw

    Passenger

  • Members
  • 4 posts

Posted 29 January 2014 - 11:32 PM

Hi

 

I have some problems with a Rails 4 query.

I'd like to check if a status is 0 or a parent_id is equal to the current ID, so I tried to:

 

Device.where{(parent_id == @parent.id) |(status == 0)}

 

I'm not getting back my records, but a <ActiveRecord::QueryMethods::WhereChain instead.

 

Is there another way to get this query to work?

 

Thanks

 



#2 keaglin

keaglin

    Passenger

  • Members
  • 4 posts

Posted 30 January 2014 - 05:48 AM

Did you copy/paste that? Logical or is double bar/pipe.

#3 reyntjensw

reyntjensw

    Passenger

  • Members
  • 4 posts

Posted 30 January 2014 - 10:03 PM

Hi Keaglin

 

I did mis a pipe sign, but that did not solve my problem.

I'm still having the same problem.



#4 reyntjensw

reyntjensw

    Passenger

  • Members
  • 4 posts

Posted 01 February 2014 - 06:15 PM

I was able to solve the problem using the SQL way and not the rails way

 

Device.where("parent_id =" +  @parent.id.to_s + " OR status = 0")


#5 Ohm

Ohm

    Guard

  • Members
  • 179 posts
  • LocationCopenhagen

Posted 02 February 2014 - 02:27 PM

I haven't seen this done directly in AR, only using SQL, as reyntjensw says.

 

Be however aware that you should never, ever, interpolate data into your SQL queries like the above. Instead do this: 

Device.where("parent_id = ? OR status = ?", @parent.id, 0)

This will sanitise and interpolate the data for you, one for each question mark. 


Blog: http://ohm.sh | Twitter: madsohm






Also tagged with one or more of these keywords: Active Record, OR, where

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users