关于多对多关联:id 未保存到数据库
About Many-to-Many Associations: id is not saving to database
我是 rails 的初学者,我正在学习 Beginning Rails 4 第 3 版:
我的rails'版本是4.2.4,Windows8.1,Ruby是2.1.6.
我有 3 个丰富的多对多关联模型:
1 条评论
2-条
3- 用户
class Comment < ActiveRecord::Base
belongs_to :article
end
class Article < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :body
belongs_to :user
has_and_belongs_to_many :categories
has_many :comments
def long_title
"#{title} - #{published_at}"
end
end
class User < ActiveRecord::Base
has_one :profile
has_many :articles, -> {order('published_at DESC, title ASC')},
:dependent => :nullify
has_many :replies, :through => :articles, :source => :comments
end
我想问你的问题是,当我尝试通过这个关联创建评论时,创建的评论的id为nil,因此没有保存到数据库中。
例如,我在 rails 控制台中尝试了以下操作。
article.comments.create(name: 'Amumu', email: 'amu@daum.net', body: 'Amumu is lonely')
我得到了以下结果。
#<Comment id: nil, article_id: 4, name: "Amumu", email: "amu@daum.net", body: "Amumu is lonely", created_at: nil, updated_at: nil>
为什么评论的 ID 变成了 nil?我希望它有一个自动生成的id,所以保存到数据库中。
尝试使用 create!
而不是 create
- 它会显示所有错误。
此外,我认为您应该在文章模型中添加 accepts_nested_attributes_for :comments
,在用户模型中添加 accepts_nested_attributes_for :articles
编辑:
请显示您来自评论和文章控制器以及新评论和新文章表单的代码。
在考虑了我在这里收到的一些评论后,我才明白我的错误是什么。
其实我的评论模型是这样的。
class Comment < ActiveRecord::Base
belongs_to :article
validates_presence_of :name, :email, :body
validate :article_should_be_published
def article_should_be_published
errors.add(:article_id, "isn't published yet") if article && !article.published?
end
end
是的,我忘了我在 Comment 模型中放置了一个验证方法。由于此验证方法,不会保存 'published_at' 属性中没有值的任何评论。
我是 rails 的初学者,我正在学习 Beginning Rails 4 第 3 版: 我的rails'版本是4.2.4,Windows8.1,Ruby是2.1.6.
我有 3 个丰富的多对多关联模型:
1 条评论
2-条
3- 用户
class Comment < ActiveRecord::Base
belongs_to :article
end
class Article < ActiveRecord::Base
validates_presence_of :title
validates_presence_of :body
belongs_to :user
has_and_belongs_to_many :categories
has_many :comments
def long_title
"#{title} - #{published_at}"
end
end
class User < ActiveRecord::Base
has_one :profile
has_many :articles, -> {order('published_at DESC, title ASC')},
:dependent => :nullify
has_many :replies, :through => :articles, :source => :comments
end
我想问你的问题是,当我尝试通过这个关联创建评论时,创建的评论的id为nil,因此没有保存到数据库中。
例如,我在 rails 控制台中尝试了以下操作。
article.comments.create(name: 'Amumu', email: 'amu@daum.net', body: 'Amumu is lonely')
我得到了以下结果。
#<Comment id: nil, article_id: 4, name: "Amumu", email: "amu@daum.net", body: "Amumu is lonely", created_at: nil, updated_at: nil>
为什么评论的 ID 变成了 nil?我希望它有一个自动生成的id,所以保存到数据库中。
尝试使用 create!
而不是 create
- 它会显示所有错误。
此外,我认为您应该在文章模型中添加 accepts_nested_attributes_for :comments
,在用户模型中添加 accepts_nested_attributes_for :articles
编辑:
请显示您来自评论和文章控制器以及新评论和新文章表单的代码。
在考虑了我在这里收到的一些评论后,我才明白我的错误是什么。
其实我的评论模型是这样的。
class Comment < ActiveRecord::Base
belongs_to :article
validates_presence_of :name, :email, :body
validate :article_should_be_published
def article_should_be_published
errors.add(:article_id, "isn't published yet") if article && !article.published?
end
end
是的,我忘了我在 Comment 模型中放置了一个验证方法。由于此验证方法,不会保存 'published_at' 属性中没有值的任何评论。