ruby on rails 3 - simple search within an array -
i want able search within micropost model. microposts have artist , song attributes. tagging these microposts use acts_as_taggable gem.
micropost model:
def self.search(search) arel = order('created_at') arel = arel.where('upper(artist) upper(?) or upper(song) upper(?)', "%#{search}%", "%#{search}%").order('created_at') if search.present? arel end
this code allows me search artist , song how can search tags?
micropost.find(1).tags returns [#actsastaggableon::tag id: 18, name: "rock", #actsastaggableon::tag id: 3, name: "rap"]
micropost.find(1).tags.map(&:name) returns array ["rock", "rap"]
how can query micropost's tags isn't in it's table artist , song? thank you.
something adding where() like?
where(self.tags.map(&:name), 'like upper(?)', "%#{search}%")
can include link gem you're using? there few out there. 1 has built in: https://github.com/mbleigh/acts-as-taggable-on
speaking more generally, tags best implemented separate table joined objects opposed stored objects themselves. way can join tags table find objects.
more specifically, example near end looks backwards. have:
where(self.tags.map(&:name), 'like upper(?)', "%#{search}%")
but (imho) should trying start tag , map object. like:
tag.where('like upper(?)', "%#{search}%").microposts
Comments
Post a Comment