ruby on rails 3 - Trying to create a complex AR query that includes associations -


i have story model has :category attribute. story in habtm relationship tag.

class tag < activerecord::base      has_and_belongs_to_many :stories, :uniq => true      validates   :name,   :presence => true     validates   :creator_id,  :presence => true  end 

class story < activerecord::base    attr_accessible :title, :category    belongs_to    :user   has_and_belongs_to_many :tags, :uniq => true end 

i want search stories either (i) have category in passed category_array or (ii) have associated tag.name in passed tag_array.

the first part straight-forward:

story.where(:category => category_array) 

it's second part stretching abilities. appreciated!

update

this solves problem , pretty easy read, best approach? btw, had thought of using arel, still don't understand how use arel across join table:

story.includes(:tags).where("tags.name in (tag_array) or category in (cat_array)") 

this solves problem , pretty easy read, best approach?

story.includes(:tags).where("tags.name in (tag_array) or category in (cat_array)") 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -