ruby-on-rails - Rails 4 多对多关联不起作用

标签 ruby-on-rails ruby associations ruby-on-rails-4 has-and-belongs-to-many

这里是 Ruby on Rails 新手。尝试创建入门博客应用程序,但在处理我的模型之间的多对多关联时遇到了问题。

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

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

我的代码如下。

感谢您对此的意见。

后.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

后 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

帖 subview (用于创建新帖子):

<%= 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 %>

谢谢, 迈克

最佳答案

当使用 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

实现此方法允许您根据需要向 categoriespost 模型添加更多属性。

关于ruby-on-rails - Rails 4 多对多关联不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473044/

相关文章:

ruby-on-rails - Rails View DRYness - 你是在 View 中设置变量还是只创建干净的方法?

ruby-on-rails - 如何从哈希中删除一个键并在 Ruby/Rails 中获取剩余的哈希?

ruby - 是否有将 R 集成到 ruby​​ 中的库?

UML类图: What is the meaning of attributes at lines?

ruby-on-rails - 更好的关联性能

ruby-on-rails - 通过 SHA1 而不是 id 的 Rails 关联

ruby-on-rails - 用于更新记录的 Rails 更新方法不起作用

ruby-on-rails - ruby rails : How to set a database timeout in application configuration?

ruby - 如何将事件作为综合浏览量发送给Google Analytics(分析)

ruby - 使用 jekyll 插件将元数据添加到液体模板