[Rails] 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::Baseattr_accessible :email, :published, :result_id, :text, :urlbelongs_to :resultdef self.not_filtered(keywords)where(['text NOT IN (?)', keywords]) if keywords.any?endThis 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@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
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.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home