PostモデルとTagモデルは中間テーブルを挟んで関連しています。
class Post < ActiveRecord::Base has_many :post_tags has_many :tags, :through => :post_tags end class Tag < ActiveRecord::Base has_many :post_tags has_many :posts , :through => :post_tags end class PostTag < ActiveRecord::Base belongs_to :post belongs_to :tag end
そこで以下のように@tagsにすべてのタグが格納しindexアクションで表示できるようにしたのですが、tagsメソッドが定義されていないというエラーが返ってきます。
リレーションは問題と思うのですが、どうすれば良いのでしょうか?
class PostsController < ApplicationController # GET /posts # GET /posts.json def index @posts = Post.all @tags = @posts.tags end end undefined method `tags' for #<Post::ActiveRecord_Relation:0x007fde8dfc4518>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。