Re: [Rails] where condition with array

2013-02-26 Thread tamouse mailing lists
On Tue, Feb 26, 2013 at 2:42 PM, Matt Jones wrote: > Plan.where('user_id NOT IN ?', [3,4,7]) Slight correction: Plan.where('user_id NOT IN (?)', [3,4,7]) -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group an

Re: [Rails] where condition with array

2013-02-26 Thread Matt Jones
On Monday, 25 February 2013 22:23:48 UTC-5, Bruno Santos wrote: > > Try something like this: > > Plan.where("user_id not in (#{[3,4,7].join(', ')})") > > This easier, and won't risk SQL injection if that array has user-generated content: Plan.where('user_id NOT IN ?', [3,4,7]) --Matt Jones

Re: [Rails] where condition with array

2013-02-26 Thread Joe James
You can always use the underlying arel_table: Plan.where(arel_table[:user_id].not_eq([3,4,7])) Saving off what the user_ids of [3,4,7] mean might make it a bit clearer too: declarative_name = arel_table[:user_id].not_eq(ids) Plane.where(declarative_name) Or succinctly in a scope: scope :decl

Re: [Rails] where condition with array

2013-02-25 Thread Bruno Santos
Try something like this: Plan.where("user_id not in (#{[3,4,7].join(', ')})") 2013/2/25 tamouse mailing lists > On Sun, Feb 24, 2013 at 8:43 PM, Javier Quarite > wrote: > > > > Have you considered doing a sql query? > > > > my approach would be this > > > > query_array = [] > > [3,4,7].each

Re: [Rails] where condition with array

2013-02-24 Thread tamouse mailing lists
On Sun, Feb 24, 2013 at 8:43 PM, Javier Quarite wrote: > > Have you considered doing a sql query? > > my approach would be this > > query_array = [] > [3,4,7].each do |value| > query_array << "user_id != #{value}" > end > > Plan.where("#{query_array}.join(" and ")") > > Also, the past week I fou

Re: [Rails] where condition with array

2013-02-24 Thread Javier Quarite
Have you considered doing a sql query? my approach would be this query_array = [] [3,4,7].each do |value| query_array << "user_id != #{value}" end Plan.where("#{query_array}.join(" and ")") Also, the past week I found this gist from ryan https://gist.github.com/ryanb/4974414 I'm still analyz

[Rails] where condition with array

2013-02-24 Thread Soichi Ishida
Rails 3.2.11 Say, my app needs special conditions like plans = Plan.arel_table @plan = Plan.where( plans[:user_id].not_eq(3) ).where( plans[:user_id].not_eq(4)