如何解决<activerecord :: associations :: collectionproxy [] =“”>错误?</activerecord :: associations :: collectionproxy>

时间:2014-08-17 16:25:23

标签: ruby-on-rails ruby ruby-on-rails-4 rails-migrations

我在两个表&#39;项目&#39;之间创建了一个关联。和&#39;价格&#39;。在他们各自的模型中我写了

class Project < ActiveRecord::Base
 validates :name, :tagline,  presence: true
 has_many :prices
end

..和

class Price < ActiveRecord::Base
 belongs_to  :project, :foreign_key => 'project_id'
end

但是当我检查rails控制台时,例如

=> project = Project.last
=> project.prices

显示以下错误

=> #<ActiveRecord::Associations::CollectionProxy []>

..和

=>project.prices.count
=>0

有什么遗漏?提前致谢

1 个答案:

答案 0 :(得分:1)

这不是错误。根据Rails的说法,它是

  

“...持有关联的对象(称为@owner)与实际关联对象(称为@target ......)之间的中间人......”

http://edgeapi.rubyonrails.org/classes/ActiveRecord/Associations/CollectionProxy.html