I have 4 models i'm dealing with. I have an Account, Location, Tag, and Tagging Model. I have set it up like follows
class Tag < ActiveRecord::Base
# belongs_to :shelter
has_many :taggings, :dependent => :destroy
has_many :locations, :through => :taggings
has_many :accounts, :through => :taggings
end
class Tagging < ActiveRecord::Base
belongs_to :location
belongs_to :tag
belongs_to :shelter
end
class Account < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings, :dependent => :destroy
end
class Location < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :tags, :through => :taggings, :dependent => :destroy
end
create_table :taggings, :force => true do |t|
t.references :account
t.references :location
t.references :tag
t.timestamps
end
The problem I'm having is when I create the form it is on the Location Page. I want to be able to tag a location but have it associated with an account and am struggling with the logic of how to do the form and controller logic correctly
In the Form I have, /location/1/tags nested form. But in the controller I can't seem to figure out how to add the tag correctly. Here is my TagsController
def create
@tag = Tag.find_or_create_by_name(params[:tag][:name])
@location = @current_account.locations.find(params[:location_id])
@location.tags << @tag
end
It is working kinda, but creating multiple rows. I want to be able to create the Tag then assign the Location, Account, Tag to the Tagging.