0

ActiveRecordクラスがあります:

class Post < ActiveRecord::Base
  has_many :posts_tags
  has_many :tags, :through => :posts_tags
end

class Tag < ActiveRecord::Base
  has_many :posts_tags
  has_many :posts, :through => :posts_tags  
end
class PostsTags < ActiveRecord::Base
  belongs_to :posts
  belongs_to :tags
end

投稿タグを取得したい場合:

 <% @posts = Post.all %>
    <% @posts.each do |post| %>
       <% if post.tags.count != 0 %>
          <div class="post-tags">
          <% post.tags.each do |tag| %>
             <span><%= tag.name%></span>
          <%end%>
          </div>    
       <%end%>
    <% end %>

エラーが発生しuninitialized constant Post::PostsTag ますこの問題を解決するにはどうすればよいですか?

4

3 に答える 3

1

渡す名前はbelongs_to単数である必要があります。

belongs_to :post
belongs_to :tag

編集

また、モデルには。という名前を付ける必要がありますPostsTag。ActiveRecordは、すべてのモデル名が単数形であり、複数形のテーブル名と一致することを想定しています。テーブル名が「posts_tags」であることを確認してください。

于 2012-08-30T13:48:16.807 に答える
1

ActiveRecordドキュメントの引用:

多対多の関係を構築する方法を選択することは必ずしも簡単ではありません。リレーションシップモデルを独自のエンティティとして使用する必要がある場合は、has_many:throughを使用してください。レガシースキーマを操作する場合、またはリレーションシップ自体を直接操作することがない場合は、has_and_belongs_to_manyを使用します。

結合にのみクラスを使用しているように見えるPostsTagsため、に切り替えることを検討する必要がありますhas_and_belongs_to_many

class Post < ActiveRecord::Base
  has_and_belongs_to_many :tags       # foreign keys in the join table
end
class Tag < ActiveRecord::Base
  has_and_belongs_to_many :posts    # foreign keys in the join table
end

クラスの列を含むposts_tagsテーブルがすでにあるはずです。そうでない場合は、作成する必要があります。関係を宣言するときに結合テーブルのカスタム名を指定しない限り、テーブルには。という名前を付ける必要があります。可能な移行は次のようになります。post_idtag_idPostsTagsposts_tags

class CreatePostsTagsJoinTable < ActiveRecord::Migration
  def change 
    create_table :posts_tags, :id => false do |t|
      t.integer :post_id
      t.integer :tag_id
    end
  end
end

app/models/posts_tags.rb次に、ファイルを削除する必要があります。これはhas_and_belongs_to_many、「結合テーブルに主キーまたはモデルが関連付けられていない」という関係があるためです。-ActiveRecord :: Associations :: ClassMethods

このメソッドのドキュメントには、結合テーブルの命名とパフォーマンスの最適化に関する詳細情報が記載されています。

于 2012-08-30T14:15:37.877 に答える
1

あなたは多対多の関係を持っており、アクティブレコードアソシエーションのガイドを読む必要があります

あなたが持っているものはそれほど難しいことではありませんが、いくつかの研究が必要です。簡単なルールとして、関連付けを使用する場合(たとえば、PostTagに説明を保存する場合)、次のコードを使用します。そうでない場合:has_and_belongs_to_many以前の投稿ですでに取り上げたものを使用してください。コードの下に、移行、モデル、およびビューがあります。

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

そして、移行は次のようになります。

class CreateTables < ActiveRecord::Migration
  def self.up
    create_table :tags do |t|
      # some other attributes here

      t.timestamps
    end

    create_table :posts do |t|
      # some other attributes here

      t.timestamps
    end

    create_table :post_tags do |t|
      # some other attributes here
      t.references :post
      t.references :tag

      t.timestamps
    end
  end

  def self.down
    drop_table :tags
    drop_table :posts
    drop_table :post_tags
  end
end

そして、あなたの見解は次のようになります。

<% @posts = Post.all %>
    <% @posts.each do |post| %>
       <% if post.tags.any? %>
          <div class="post-tags">
          <% post.tags.each do |tag| %>
             <span><%= tag.name %></span>
          <%end%>
          </div>    
       <%end%>
    <% end %>
于 2012-08-30T14:27:46.060 に答える