4

Ruby on rails 新手在这里。尝试创建一个入门博客应用程序,但我的模型之间存在多对多关联问题。

我有 2 个模型 - 帖子、类别,它们之间有多对多的关联。

我的问题:当我创建一个新帖子时,帖子被保存但后类别关联没有保存在 categories_posts 表中。

我的代码如下。

感谢您对此的投入。

post.rb

class Post < ActiveRecord::Base
  validates_presence_of :title, :body, :publish_date
  belongs_to :user
  has_and_belongs_to_many :categories
end

类别.rb

class Category < ActiveRecord::Base
  validates_presence_of :name
  has_and_belongs_to_many :posts
end

categories_posts.rb

class CategoriesPosts < ActiveRecord::Base
end

迁移 - create_posts.rb

class CreatePosts < ActiveRecord::Migration
  def change
    create_table :posts do |t|
     t.string :title
     t.text :body
     t.date :publish_date
     t.integer :user_id

     t.timestamps
    end
  end
end 

迁移 - create_categories.rb

class CreateCategories < ActiveRecord::Migration
  def change
    create_table :categories do |t|
      t.string :name
      t.timestamps
    end 
  end
end

迁移 - create_categories_posts.rb

class CreateCategoriesPosts < ActiveRecord::Migration
  def change
    create_table :categories_posts do |t|
      t.integer :category_id
      t.integer :post_id
      t.timestamps
    end
  end
end

Post Controller - 创建和新方法

#GET /posts/new
def new
  @post = Post.new
end

def create
  @post = Post.new(post_params)

  #User id is not a form field and hence is not assigned in the view. It is assigned when control is transferred back here after Save is pressed
  @post.user_id = current_user.id

  respond_to do |format|
    if @post.save
      format.html { redirect_to @post, notice: 'Post was successfully created.' }
      format.json { render action: 'show', status: :created, location: @post }
    else
      format.html { render action: 'new' }
      format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

帖子视图(用于创建新帖子):

<%= simple_form_for @post, :html => { :class => 'form-horizontal' } do |f| %>
  <%= f.input :title %>
  <%= f.input :body %>
  <%= f.input :publish_date %>
  <%= f.association :categories, :as => :check_boxes %>
  <div class="form-actions">
    <%= f.button :submit, :class => 'btn-primary' %>
    <%= link_to t('.cancel', :default => t("helpers.links.cancel")),
            posts_path, :class => 'btn' %>
  </div>
<% end %>

谢谢,迈克

4

2 回答 2

14

使用has_and_belongs_to_many关联时,您需要连接表上的唯一索引。您的迁移应如下所示:

class CreateCategoriesPosts < ActiveRecord::Migration
  def change
    create_table :categories_posts do |t|
      t.integer :category_id
      t.integer :post_id
      t.timestamps
    end
    add_index :categories_posts, [:category_id, :post_id]
  end
end

您还可以摆脱 CategoriesPost 模型,仅当您想实现:has_many, :through关联时才需要该模型。那应该回答你的问题。


并且为了彻底,如果您想使用:has_many, :through与 CategoriesPost 模型的关联,您可以像这样实现:

class Post < ActiveRecord::Base
  has_many :categoriesposts
  has_many :categories, :through => :categoriesposts
end
class Category < ActiveRecord::Base
  has_many :categoriesposts
  has_many :posts, :through => :categoriesposts
end
class CategoriesPost < ActiveRecord::Base
  belongs_to :post
  belongs_to :category
end

如果需要,实现此方法可让您向 categorypost 模型添加更多属性。

于 2013-10-20T07:04:46.867 回答
0

除了第一个答案,您需要将关联放在您的连接模型(CategoriesPosts)中,如下所示:

Class CategoriesPosts  < ActiveRecord::Base
    belongs_to :category
    belongs_to :post
End
于 2013-10-20T08:08:42.903 回答