关于ActiveRecord :: Associations :: CollectionProxy为什么没有save_all方法?

时间:2014-11-04 11:13:59

标签: ruby-on-rails ruby activerecord associations

ActiveRecord::Associations::CollectionProxy课程只有delete_alldestroy_all方法,我无法查看save_all方法,但我使用" build"要初始化许多对象,我想保存一次。

1 个答案:

答案 0 :(得分:1)

update_all但没有save_all

您可以这样使用update_all

# Update all billing objects with the 3 different attributes given
Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" )

# Update records that match our conditions
Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" )

# Update records that match our conditions but limit it to 5 ordered by date
Billing.update_all( "author = 'David'", "title LIKE '%Rails%'",
                      :order => 'created_at', :limit => 5 )

如果您使用build初始化了许多对象,那么您可以像这样保存它们(如果它们都在数组中):

initialized_objects.each(&:save)