Hi all, This is my first question, and hopefully someone can help. Overview:
models:....................................................................... 1. class Post < ActiveRecord::Base belongs_to :user has_and_belongs_to_many :categories acts_as_mappable :distance_field_name => :distance
2. class Category < ActiveRecord::Base has_and_belongs_to_many :posts
3. and, of course, the join table: categories_posts
the search:....................................................................... The following find works and is paginated via will_paginate:
results = Post.paginate(:page => page, :origin => user, :conditions => ['removed_at IS NULL AND zip = ? AND intention = ? AND title like ?', zip, search_type, "%#{search}%"], :order => 'date_format(updated_at,"%Y%m%d") DESC, distance ASC' ) if search.blank?
details: search --- the query enter by the user search_type --- selected by the user from ['offered', 'wanted'] removed_at --- date/time post was made invisible zip -- posts.zip intention -- posts.intention is either offered or wanted matches search_type :origin => user --- references the current user's latitude/longitude for acts_as_mappable, which calculates a distance column (i.e. distance of post from current user)
problem: ........................................................................ The search needs to include only rows that also match one or more categories ???
I have tried :join and :include(this can't have conditions), but I can't seem to make it work properly. note: the results can be read-only as it's just a search If I used find_by_sql I think I would lose acts_as_mappable(GeoKit) and will_paginate functionality.
Any help or suggestions would be greatly appreciated. thanks, chris