轨道4多对多关联不工作 [英] Rails 4 many to many association not working
问题描述
Ruby on Rails的新手在这里。试图创造一个启动博客应用程序,并有许多麻烦,我的模型之间有许多关联。
Ruby on rails newbie here. Trying to create a starter blog app and having trouble with many to many association between my models.
我有2个型号 - 邮政,类别具有许多彼此之间有很多关联
I have 2 models - Post, Category that have a many to many association between each other.
我的问题:当我创建了一个新的职位,岗位被保存,但该类别后的关联没有得到保存在categories_posts表
My problem: When I create a new post, the Post gets saved but the post-category association does not get saved in the categories_posts table.
我的code是如下图所示。
My code is as below.
我AP preciate你对这个投入。
I appreciate your inputs on this.
post.rb
class Post < ActiveRecord::Base
validates_presence_of :title, :body, :publish_date
belongs_to :user
has_and_belongs_to_many :categories
end
category.rb
category.rb
class Category < ActiveRecord::Base
validates_presence_of :name
has_and_belongs_to_many :posts
end
categories_posts.rb
categories_posts.rb
class CategoriesPosts < ActiveRecord::Base
end
迁移 - create_posts.rb
Migrations - 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
Migrations - 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
Migrations - 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 - create and new methods
#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
后视图(用于创建新邮报):
Post View(for creating a new Post):
<%= 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 %>
谢谢,
迈克
Thanks, Mike
推荐答案
在使用你需要在你的连接表的唯一索引的 has_and_belongs_to_many
关联。迁移应该是这样的:
When using the has_and_belongs_to_many
association you need a unique index on your join table. Your migration should look like this:
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,:通过
关联。这应该回答你的问题。
You can also get rid of the CategoriesPost model, that is only needed if you wanted to implement a :has_many, :through
association. That should answer your question.
和刚需彻底的,如果你想使用:的has_many,:通过
与CategoriesPost模型关联,你可以实现像这样:
And just to be thorough, if you wanted to use a :has_many, :through
association with a CategoriesPost model you can implement that like so:
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模式。
Implementing this method allows you to add more attributes to your categoriespost model if you wanted.
这篇关于轨道4多对多关联不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!