Rails 4多对多关联不起作用
Ruby on rails新手在这里。 试图创建一个入门博客应用程序,并在我的模型之间的多对多关联中遇到问题。
我有2个模型 – post,类别,彼此之间有多对多关联。
我的问题:当我创建一个新post时,Post会被保存,但是类别后关联不会保存在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
发布视图(用于创建新post):
{ :class => 'form-horizontal' } do |f| %> :check_boxes %> 'btn-primary' %> t("helpers.links.cancel")), posts_path, :class => 'btn' %>
谢谢,迈克
使用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
才需要它。 这应该回答你的问题。
而且要彻底,如果你想使用: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模型添加更多属性。
在第一个答案之后,您需要将关联放在您的连接模型(CategoriesPosts)中,如下所示:
Class CategoriesPosts < ActiveRecord::Base belongs_to :category belongs_to :post End
- Rails根据父模型属性过滤子模型的记录
- 外键(class_id)未在belongs_to关联中填充
- 使用mongodb作为数据库的rails中的关联
- 多对多连接和关联
- Rails + MongoID +简单forms关联:#Mongoid :: Relations :: Metadata的未定义方法`options’
- Rails:ActiveRecord :: HasManyThroughSourceAssociationNotFoundError:找不到源关联
- 为什么这个rails关联在急切加载后单独加载?
- Ruby on Rails 3:结合多个has_many或has_many_through关联的结果
- 是否可以只询问ActiveRecord关联中的某些列?