ruby-on-rails – 与ActiveRecord的HABTM关系的时间戳
我有以下关系设置:
class Article < ActiveRecord::Base has_and_belongs_to_many :authors end class Author < ActiveRecord::Base has_and_belongs_to_many :articles end 我注意到,虽然连接表article_authors具有时间戳,但是在创建新关系时不会填充.例如: Author.first.articles << Article.first 重要的是我会跟踪作者与文章的关联. 解决方法
从
rails guides.
class Article < ActiveRecord::Base has_many :article_authors has_many :authors,:through => :article_authors end class Author < ActiveRecord::Base has_many :article_authors has_many :articles,:through => :article_authors end class ArticleAuthor < ActiveRecord::Base belongs_to :article belongs_to :author end 如果它仍然不适用于该结构,那么不用使用数组推,使用一个create. Author.first.article_authors.create(:article => Article.first) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |