railsでタグ機能をつけようとしてtableを関連付けしようとしています。
下記の記述でtag_mapsを中間にatableを設計しようとしているのですが
中間テーブル class TagMap < ApplicationRecord belongs_to :post belongs_to :tag validates :post_id,presence:true validates :tag_id,presence:true end class Tag < ApplicationRecord validates :name,presence:true,length:{maximum:50} has_many :posts, through: :tag_maps has_many :tag_maps, dependent: :destroy end class Post < ApplicationRecord has_many :tags, through: :tag_maps has_many :tag_maps, dependent: :destroy def save_posts(savepost_tags) current_tags = self.tags.pluck(:name) unless self.tags.nil? old_tags = current_tags - savepost_tags new_tags = savepost_tags - current_tags # Destroy old taggings: old_tags.each do |old_name| self.tags.delete Tag.find_by(name:old_name) end # Create new taggings: new_tags.each do |new_name| post_tag = Tag.find_or_create_by(name:new_name) self.tags << post_tag end end end
投稿時に下記のエラーが出てしまいます
Cannot have a has_many :through association 'Post#tags' which goes through 'Post#tag_maps' before the through association is defined.
このエラーってどういうときに出るエラーか教えてもらえると助かります。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/23 05:19