Kenny Meyer
2012-Aug-15 15:17 UTC
Defining a scope which compares a field against an array
Hello, I am defining the following scope for my model: class Entry < ActiveRecord::Base attr_accessible :email, :published, :result_id, :text, :url belongs_to :result def self.not_filtered(keywords) where([''text NOT IN (?)'', keywords]) if keywords.any? end This is supposed to return only Entries where the Entry.text doesn''t contain any of the keywords. Example: entries.not_filtered(["realtor", "quadplex", "duplex", "condo", "agent", "broker"]) But somehow this doesn''t exclude the entires which include the keywords in the array. Is there a way to write that differently? How? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/zhxFEkhmxLIJ. For more options, visit https://groups.google.com/groups/opt_out.
Kenny Meyer
2012-Aug-15 15:54 UTC
Re: Defining a scope which compares a field against an array
I think squeel (https://github.com/ernie/squeel) is the best way to go: Person.where{name.like_any names} # => SELECT "people".* FROM "people" WHERE (("people"."name" LIKE ''Ernie%'' OR "people"."name" LIKE ''Joe%'' OR "people"."name" LIKE ''Mary%'')) On Wednesday, August 15, 2012 11:17:53 AM UTC-4, Kenny Meyer wrote:> > Hello, > > I am defining the following scope for my model: > > class Entry < ActiveRecord::Base > attr_accessible :email, :published, :result_id, :text, :url > > belongs_to :result > > def self.not_filtered(keywords) > where([''text NOT IN (?)'', keywords]) if keywords.any? > end > > This is supposed to return only Entries where the Entry.text doesn''t > contain any of the keywords. > > Example: > > entries.not_filtered(["realtor", "quadplex", "duplex", "condo", "agent", > "broker"]) > > But somehow this doesn''t exclude the entires which include the keywords in > the array. > > Is there a way to write that differently? How? > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/tAo0RPmi_NAJ. For more options, visit https://groups.google.com/groups/opt_out.