私は4つのモデルを扱っています。私は、アカウント、場所、タグ、およびタグ付けモデルを持っています。私はそれが場所のページにあるフォームを作成するときに私がいる問題があるRails - 多数あり:複数のモデルが混乱している
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
を次のように私はそれを設定しています。私は場所にタグを付けることができるが、それはアカウントに関連付けられているし、フォームとコントローラロジックを正しく行う方法のロジックに苦労している。
私は/ location/1/tagsネストフォーム。しかしコントローラでは、タグを正しく追加する方法を理解できないようです。ここに私の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
それは働いていますが、複数の行を作成しています。私は、タグを作成し、タグ付けに場所、アカウント、タグを割り当てることができるようにしたいと思います。
あなたはBELONGS_TO持っているどのように来る:あなたのタグ付けモデルで避難所を。それはbelongs_to:accountではありませんか? – JosephL
ジョセフあなたは正しいです...それはカットアンドペーストでうんざりでした...アライブな試合は – bokor