轨道推入数组保存对象 [英] Rails push into array saves object

查看:164
本文介绍了轨道推入数组保存对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有趣的问题。我使用Ruby 1.9.2和Rails 3.1.3。

我有2个型号,为简化起见假设顾客和商店。 商店拥有众多客户,与客户属于一个商店。 我试图收集所有客户的商店,并创建一个地方多了一些,我可以值更高填充。相反,customer.save被调用的时候,我不指望了。

 商店= Store.find(1)
customers_array = store.customers
random_array = Array.new
customers_count = customers_array.count + 1

(customers_count..2)。每个做|我|
  客户= Customer.new
  c.id =#{I} 000000000000
  random_array<<客户#这行不叫customer.save
  customers_array<<客户#这条线叫customer.save时,店里有顾客
结束
 

有关当客户推入阵列某种原因,customer.save被调用。 如果你推到一个数组是一个普通的阵列,而不是一个关系,它不会发生。

我找到了一个解决办法,但我仍然不知道为什么发生这种情况。 解决方法:

 商店= Store.find(1)
initial_customers_array = store.customers
additional_customers_array = Array.new
customers_count = initial_customers_array.count + 1

(customers_count..2)。每个做|我|
  客户= Customer.new
  c.id =#{I} 000000000000
  additional_customers_array<<顾客
结束
customers_array = initial_customers_array + additional_customers_array
 

解决方案

<< 的别名

  • <一个href="http://apidock.com/rails/ActiveRecord/Associations/CollectionProxy/%3C%3C">http://apidock.com/rails/ActiveRecord/Associations/CollectionProxy/%3C%3C
  • <一个href="http://apidock.com/rails/ActiveRecord/Associations/CollectionProxy/push">http://apidock.com/rails/ActiveRecord/Associations/CollectionProxy/push

这在的ActiveRecord ::协会:: CollectionProxy 电话 CONCAT

  • <一个href="http://apidock.com/rails/ActiveRecord/Associations/CollectionAssociation/concat">http://apidock.com/rails/ActiveRecord/Associations/CollectionAssociation/concat (查看方法的来源)
  • <一个href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_proxy.rb#L283">https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/collection_proxy.rb#L283

这就要求 concat_records

  • <一个href="http://apidock.com/rails/v3.2.3/ActiveRecord/Associations/CollectionAssociation/concat_records">http://apidock.com/rails/v3.2.3/ActiveRecord/Associations/CollectionAssociation/concat_records

在这里你可以看到插入的发生。

因此​​,与现有记录的(持久化到数据库)的,运行&LT;&LT; .push 将插入记录的收集,他们坚持到数据库中,如果有必要的。呼叫&LT;&LT; 在一个阵列,而不是记录集,因为你做的

  random_array&LT;&LT;顾客
 

调用Ruby的&LT;&LT; 阵列方法,而不是AR等价的(如你发现没有保存发生在这种情况下)的。

编辑:需要明确的是,你找到了解决办法是多还是少怎么我通常要处理你处理的情况;我的回答更注重的为什么&LT;&LT; 有这种行为

I have an interesting problem. I'm using Ruby 1.9.2 and Rails 3.1.3.

I have 2 models, for simplification let's say customers and stores. Stores have many customers, and a customer belongs to a store. I'm trying to collect all customers for a store, and create a place for a few more that I can populate with values later. Instead, customer.save is called when I don't expect it.

store = Store.find(1)
customers_array = store.customers
random_array = Array.new
customers_count = customers_array.count + 1 

(customers_count..2).each do |i|
  customer = Customer.new
  c.id = "#{i}000000000000"
  random_array << customer # this line doesn't call customer.save
  customers_array << customer # this line calls customer.save when store has customers
end

For some reason when the customer is pushed into the array, customer.save is called. It doesn't happen if you push to an array is a plain array and not a relation.

I found a workaround, but I'm still wondering why that happens. The workaround:

store = Store.find(1)
initial_customers_array = store.customers
additional_customers_array = Array.new
customers_count = initial_customers_array.count + 1 

(customers_count..2).each do |i|
  customer = Customer.new
  c.id = "#{i}000000000000"
  additional_customers_array << customer 
end
customers_array = initial_customers_array + additional_customers_array

解决方案

<< is an alias for push

which in the ActiveRecord::Associations::CollectionProxy calls concat

which calls concat_records

where you can see the insert taking place.

So, with an existing record (persisted into the database), running << or .push will insert records into the collection, persisting them to the database if necessary. Calling << on an Array, not the record collection, as you're doing in

random_array << customer

calls Ruby's << Array method, not the AR equivalent (as you found, no save takes place in this case).

Edit: To be clear, the workaround you found is more or less how I typically handle the situation you're dealing with; my answer focuses more on why << has this behavior.

这篇关于轨道推入数组保存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆