Rails 4多对多协会不工作
Ruby on rails新手在这里。试图创建一个初学者博客应用程序,并且遇到与我的模型之间存在多对多关联的问题。Rails 4多对多协会不工作
我有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
category.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
柱控制器 - 创建和新方法
#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 %>
个
谢谢, 迈克
当使用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
协会,才需要。这应该回答你的问题。
而只是要彻底,如果你想用一个CategoriesPost模型:has_many, :through
关联中,你可以实现像这样:
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
实现这个方法可以让你把更多的属性添加到您的如果你想的话
我认为他在做HABTM,没有-many:通过。我认为你应该改变你的答案,因为它是正确的(他需要更新他的加入模式),但对于上下文可能会产生误导 –
继第一个答案,你需要把协会的加盟模式(CategoriesPosts)是这样的:
Class CategoriesPosts < ActiveRecord::Base
belongs_to :category
belongs_to :post
End
@里克佩奇和安东尼去 感谢您的意见。 根据您的建议,更新后的帖子和类别模型要分别使用has_many:categories,:through =>:categoriesposts和has_many:posts,:through =>:categoriesposts。现在,当我尝试创建新帖子时,出现“未初始化的常量Post :: Categoriespost”错误。 错误的截图如下: http://imgur.com/a/BPD9k –
我也有同样的问题,在这里找到了解决办法:https://开头计算器。com/questions/11480939/many-to-many-many-many-through-association-form-with-data-assigned-to-linking-m?answertab = active#tab-top – Eltaf