Hi,
When using acts as taggable on steroids, you can search a model’s tags
by doing somthing like
Post.find_tagged_with(‘Funny, Silly’)
But how do you search all tags without using manual sql and not limit it
just to one model?
thanks
Hi,
When using acts as taggable on steroids, you can search a model’s tags
by doing somthing like
Post.find_tagged_with(‘Funny, Silly’)
But how do you search all tags without using manual sql and not limit it
just to one model?
thanks
Hi,
When using acts as taggable on steroids, you can search a model’s tags
But how do you search all tags without using manual sql and not limit it
just to one model?
When you install the plugin you have the taggings table and the
corresponding Tagging model. Tagging is related to the model Tag.
So, you could do something like
Tagging.find(:all,:include=>:tag,:conditions=>[‘tags.name in
(?)’,tags_to_search])
That will give you all the taggings, having the model name in the
“taggable_type” attribute and the id in the taggable_id field.
Moreover, Tagging has a polymorphic reference to :taggable, meaning you
could directly get the related objet directly by invoking .taggable over
any of the row results. Just be aware you’ll be launching a new query
for each row if you do so since as far as I know you cannot eager load a
polymorphic association.
Regards,
javier ramírez
Thanks for the comments above, this is how I have done it. My concern at
the minute is the 21 database queries that need to made to get the
results required.
Has anyone got any comments on how this can be optimized?
Thanks
#
# searchers all taggable types
def do_all_search(ps_condition, ps_page)
initial_results = Tagging.paginate(:all,
:include=>[:tag],
:conditions=>['tags.name in (?)',
ps_condition],
:page => ps_page,
:per_page => 20)
actual_result = Array.new
initial_results.each do |tag_result|
actual_result <<
tag_result.taggable.class.find(tag_result.taggable.id)
end
return actual_result, initial_results
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs