0

这是示例:

@profiles = Profile.where(something: true)

而不是这样做:

@profiles.each do |profile|
  @some_user.tag(profile, :with => "paris, normandy")
end

有没有办法在一次调用中标记模型的所有实例,像这样?:

@some_user.tag(@profiles, :with => "paris, normandy")
4

1 回答 1

0

我认为没有什么开箱即用的方法,但是如果您担心性能/速度,请直接使用标记模型进行批量标记分配。

这是针对您的用例的代码改编版,您仍然需要提供 profile_ids 和 tag_ids。

Tagging.transaction do
  profile_ids.each do |pid|
    tag_ids.each do |tid|
      values = ["Profile", pid, tid].join(", ")
      Tagging.connection.execute "INSERT INTO taggings (taggable_type, taggable_id, tag_id) VALUES (#{values}) ON DUPLICATE KEY UPDATE id=id"
    end
  end
end 
于 2014-06-19T05:18:05.170 回答