【发布时间】:2015-04-25 04:39:44
【问题描述】:
我的应用程序中有这样的 HABTM 关系:
class Book < ActiveRecord::Base
has_and_belongs_to_many :authors
end
class Author < ActiveRecord::Base
has_and_belongs_to_many :books
end
在 Rails 控制台中,我可以像这样访问 Book 和 Author 的记录:
Book.all
Book.first
b = Book.first
b.title = "Title2"
b.save
...
但我不知道如何访问连接表。
如何访问和查看连接表books_authors 中的记录?
是否可以更改连接表行?
【问题讨论】:
-
你不能。
has_and_belongs_to_many使用连接 table,而不是连接 model。您想将has_many :through与连接模型一起使用。
标签: ruby-on-rails ruby-on-rails-4 join many-to-many rails-console