Ruby on rails .每个都在空集合上迭代-Ruby on Rails

Ruby on rails .每个都在空集合上迭代-Ruby on Rails,ruby-on-rails,ruby,ruby-on-rails-5,carrierwave,Ruby On Rails,Ruby,Ruby On Rails 5,Carrierwave,我有一个叫做附件的多态模型。我正在使用gem Carrierwave保存附件 在我的Customer编辑页面上,我执行以下代码: puts @customer.attachments.count @customer.attachments.each do |i| puts i.id #outputs a blank line end put@customer.attachments.count输出0。但是,迭代器仍然在附件上运行1次,并打印出一个空行来

我有一个叫做附件的多态模型。我正在使用gem Carrierwave保存附件

在我的
Customer
编辑页面上,我执行以下代码:

    puts @customer.attachments.count
    @customer.attachments.each do |i|
        puts i.id #outputs a blank line 
    end
put@customer.attachments.count
输出
0
。但是,迭代器仍然在附件上运行1次,并打印出一个空行来代替
put i.id

这是我的模型:

class Attachment < ApplicationRecord
    mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
    validates :name, presence: true

    belongs_to :attachable, :polymorphic => true
    belongs_to :account
end
类附件true
属于:帐户
结束

模型将加载它们的关联一次,例如
@customer.attachments
,然后不再查询它们。如果关联更改,
@customer.attachments
将过期。例如

# Let's say this includes Attachment 123
puts @customer.attachments

Attachment.delete(123)

# Will still include Attachment 123
puts @customer.attachments
您可以通过强制下次重新加载来手动卸载关联。更好的方法是以关联知道的方式更改关联,例如调用

这将删除附件123并将其从
@customer.attachments
中删除

puts @customer.attachments

Attachment.create( foo: "bar", customer: @customer )

# will not be aware of the new Attachment.
puts @customer.attachments
与创建关联类似的问题。这将创建附件并更新
@customer.attachments

puts @customer.attachments

Attachment.create( foo: "bar", customer: @customer )

# will not be aware of the new Attachment.
puts @customer.attachments
与前面一样,在关联上调用
create

@customer.attachments.create( foo: "bar" )

这也有很好的效果,为您填写正确的客户,避免可能的错误。而且它避免了在整个代码中重复附件类名,从而使代码变干。

模型将加载它们的关联一次,例如
@customer.attachments
,然后不再查询它们。如果关联更改,
@customer.attachments
将过期。例如

# Let's say this includes Attachment 123
puts @customer.attachments

Attachment.delete(123)

# Will still include Attachment 123
puts @customer.attachments
您可以通过强制下次重新加载来手动卸载关联。更好的方法是以关联知道的方式更改关联,例如调用

这将删除附件123并将其从
@customer.attachments
中删除

puts @customer.attachments

Attachment.create( foo: "bar", customer: @customer )

# will not be aware of the new Attachment.
puts @customer.attachments
与创建关联类似的问题。这将创建附件并更新
@customer.attachments

puts @customer.attachments

Attachment.create( foo: "bar", customer: @customer )

# will not be aware of the new Attachment.
puts @customer.attachments
与前面一样,在关联上调用
create

@customer.attachments.create( foo: "bar" )

这也有很好的效果,为您填写正确的客户,避免可能的错误。而且它避免了在整个代码中重复附件类名,从而使代码变干。

count使用sql,如果使用
.size
,那会显示1吗?如果是这样,它就在内存中,并且还没有被持久化。
。size
显示1。但是,
Attachment.all.size
显示0。数据库中不应该有任何附件。尚未在我的测试环境中创建任何@如果这是一个测试,分享它。数据库中没有任何数据,它只在内存中。尝试
@customer.attachments.first.persistend?
它将显示错误,然后附件在内存中,这就是为什么没有
id
输出(没有持久化到数据库)。尝试将其更改为
puts i.inspect
不要忘记,自从Ruby 1.9于2007年发布以来,
polymorphic:true
表示法一直受到支持。这与其他语言处理“dictionary”类型结构的方式更为一致,因为它不太详细,所以值得使用。count使用sql,如果您使用
。size
-是否显示1?如果是这样,它就在内存中,并且还没有被持久化。
。size
显示1。但是,
Attachment.all.size
显示0。数据库中不应该有任何附件。尚未在我的测试环境中创建任何@如果这是一个测试,分享它。数据库中没有任何数据,它只在内存中。尝试
@customer.attachments.first.persistend?
它将显示错误,然后附件在内存中,这就是为什么没有
id
输出(没有持久化到数据库)。尝试将其更改为
puts i.inspect
不要忘记,自从Ruby 1.9于2007年发布以来,
polymorphic:true
表示法一直受到支持。这更符合其他语言处理“dictionary”类型结构的方式,因为它不太冗长,所以值得使用。