Defining a scope which compares a field against an array

12 views
Skip to first unread message

Kenny Meyer

unread,
Aug 15, 2012, 11:17:53 AM8/15/12
to rubyonra...@googlegroups.com
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?

Kenny Meyer

unread,
Aug 15, 2012, 11:54:01 AM8/15/12
to rubyonra...@googlegroups.com
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%'))
Reply all
Reply to author
Forward
0 new messages