Hi to all,
I’m needing some help here … I want to do an extension on a
has_and_belongs_to_many to build a custom add method to the
i have a table locations that has a many-to-many association with a
tags tabel using a locations_tags table.
i want to add a method to the Location has_and_belongs_to_many
association so that i can do something like this on the location
myLocation.tags.add_by_names ‘tag1 tag2 tag3’
On the extension i want to do something like this:
class Location < ActiveRecord::Base
has_and_belongs_to_many :tags do
def add_by_names tag_names
tag_names.split(" ").each do |tag_name|
tag = Tag.find_by_name(tag_name) || Tag.create(:name =>
tags << tag unless tags.find(tag.id)
The problem is, that i cant feager out how to have access to location
tags collection inside my custom create method.
Is there a way to do this?
any help or point to some examples welcome.