Rails has_and_belongs_to_many 总是插入到数据库中

发布于 2024-12-21 07:17:09 字数 545 浏览 4 评论 0原文

这是我的问题:

class Facility < ActiveRecord::Base
...
has_and_belongs_to_many :languages, :autosave => false, :join_table => 'facilities_languages'
... 
end

当我做这样的事情时:

facility = Facility.find(1)
language = Language.find(1)
facility.languages << language

Rails 总是执行 SQL 请求:

"INSERT INTO `facilities_languages` (`language_id`,`facility_id`) VALUES (1, 1)"

有没有办法避免数据库请求,除非我调用 'facility.save' ?

显然, :autosave 选项在这里还有其他作用。

Here is my problem:

class Facility < ActiveRecord::Base
...
has_and_belongs_to_many :languages, :autosave => false, :join_table => 'facilities_languages'
... 
end

When I do something like this:

facility = Facility.find(1)
language = Language.find(1)
facility.languages << language

Rails always do the SQL request:

"INSERT INTO `facilities_languages` (`language_id`,`facility_id`) VALUES (1, 1)"

Is there a way to avoid database requests unless I call 'facility.save' ?

Apparently, :autosave option here does something else.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深居我梦 2024-12-28 07:17:09

您的问题是您使用的 has_and_belongs_to_many 不是基于 Model 的,因此您无法将其作为对象访问。您可以创建一个执行关联的模型,然后您可以创建该关联模型的新对象。例如:

rails g scaffold JoinClass faculty_id:integer language_id:integer

在模型中

class JoinClass < ActiveRecord::Base
     belongs_to :faculty
     belongs_to :language
end

在其他模型中:

Faculty

class Faculty < ActiveRecord::Base
     has_many :join_classes
     has_many :languages, :though => :join_classes
end

Language

class Language < ActiveRecord::Base
     has_many :join_classes
     has_many :faculties, :through => :join_classes
end

那么您将能够通过创建不自动保存的关联对象来添加关联。例如,您有一个包含 ids 的 String,其中包含应在 FacultyController 中名为 language_ids 关联的语言对象的 id

language_ids.split(",").each do |language_id|
    JoinClass.create(:faculty_id => @faculty.id, :language_id => language_id)
end

:代码应该在系统保存后进行调整和定位。

Your Problem is that you use has_and_belongs_to_many which isnt based on a Model so you cant access it as an Object. You could create a Model that is doing the associations and then you could create new objects of this association model. e.g.:

rails g scaffold JoinClass faculty_id:integer language_id:integer

In the Model

class JoinClass < ActiveRecord::Base
     belongs_to :faculty
     belongs_to :language
end

In the other models:

Faculty

class Faculty < ActiveRecord::Base
     has_many :join_classes
     has_many :languages, :though => :join_classes
end

Language

class Language < ActiveRecord::Base
     has_many :join_classes
     has_many :faculties, :through => :join_classes
end

Then you would be able to add associations by creating association objects which arent saved automaticly. For example you have a String containing ids that contains the ids of the language objects which should be associated called language_ids in the FacultyController:

language_ids.split(",").each do |language_id|
    JoinClass.create(:faculty_id => @faculty.id, :language_id => language_id)
end

Of cause this code should be conditioned and positioned after the save of faculty.

百合的盛世恋 2024-12-28 07:17:09

如果您使用铲子操作符<< Rails 自动保存关联的对象。

从文档中
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods。 html

集合<<(对象,...)

将一个或多个对象添加到集合中
在连接表中创建关联(collection.push 和
collection.concat 是此方法的别名)。请注意,这
操作立即触发更新 sql,无需等待保存或
对父对象的更新调用。

If you use the shovel operator << Rails automatically saves the associated object.

From the documentation
http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html

collection<<(object, …)

Adds one or more objects to the collection by
creating associations in the join table (collection.push and
collection.concat are aliases to this method). Note that this
operation instantly fires update sql without waiting for the save or
update call on the parent object.

殊姿 2024-12-28 07:17:09

设施.语言 = [语言]

facility.languages = [language]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文