squeel icon indicating copy to clipboard operation
squeel copied to clipboard

eq_all on a joined model?

Open jgrannas opened this issue 9 years ago • 2 comments

What am I doing wrong here... Is this not possible with squeel? I am on rails 4.2, squeel 1.2.3, and postgresql

Class Product < ActiveRecord::Base

scope :with_squeel, -> (slug_array) {
    joins{tags}
    .where{ (tags.slug.eq_all slug_array) }
  }

Basically I want to pass in an array of tag names, and only return records that has all the associated tag names. I've also tried in_all which doesnt return my Products that have both tags.

Ex: Product.with_squeel(["white","plastic"])

jgrannas avatar Apr 10 '15 17:04 jgrannas

anyone with experience doing something like this that can chime in? Am I doing something wrong or is this not possible or an issue with the Squeel?

jgrannas avatar Apr 13 '15 18:04 jgrannas

check the "Sifters" parts on the readme. You can do something like this:

class Article < ActiveRecord::Base
  sifter :title_or_body_contains do |string|
    title.matches("%#{string}%") | body.matches("%#{string}%")
  end
end

Other option is to create a static method with that name.

agrass avatar Apr 13 '15 18:04 agrass