Skip to content

Instantly share code, notes, and snippets.

@meesterdude
Created April 19, 2012 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meesterdude/2424738 to your computer and use it in GitHub Desktop.
Save meesterdude/2424738 to your computer and use it in GitHub Desktop.
trying to get @product_results to not contain products who are already in a given @category
class Product < ActiveRecord::Base
has_and_belongs_to_many :product_groups
...
scope :in_categories, lambda {|ids| where(:category_id => ids) }
scope :visible, where(:visible => true)
scope :search, lambda {|kw| where(
['name LIKE :kw OR keywords LIKE :kw OR number LIKE :kw', :kw => "%#{kw}%"]
)}
end
class ProductGroup < ActiveRecord::Base
has_and_belongs_to_many :products
attr_accessible :name
validates :name, :presence => true
end
def add_remove
@product_group = ProductGroup.find(params[:id])
@products = @product_group.products
@product_results = Product.search(params[:search])
if params[:search].nil? || params[:search].empty?
@product_results = []
else
@product_results = Product.search(params[:search])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment