Ruby on rails 从ActiveRecord\u Associations\u CollectionProxy中删除记录
铁路:4.2 Ruby:2.2.3 我在一个模型中使用性能,它的类是:Performance::ActiveRecord\u Associations\u CollectionProxy 在从Rails 2升级到Rails 4之前,此方法有效:Ruby on rails 从ActiveRecord\u Associations\u CollectionProxy中删除记录,ruby-on-rails,ruby,activerecord,collections,Ruby On Rails,Ruby,Activerecord,Collections,铁路:4.2 Ruby:2.2.3 我在一个模型中使用性能,它的类是:Performance::ActiveRecord\u Associations\u CollectionProxy 在从Rails 2升级到Rails 4之前,此方法有效: def remove_expired_performances performances.reject! {|performance| performance.date < Date.today} end def删除过期的性能 拒绝!{| p
def remove_expired_performances
performances.reject! {|performance| performance.date < Date.today}
end
def删除过期的性能
拒绝!{| performance | performance.date
现在,我得到一个错误,拒绝!是ActiveRecord\u关联\u CollectionProxy的无效方法。这是有意义的,因为此处未列出拒绝:
Rails 4中实现相同结果(从集合中删除比今天日期更早的记录)的最干净的方法是什么
注意:我需要将对象保留为ActiveRecord\u Associations\u CollectionProxy,并命名为Performance 我假设你的模型
有很多:性能
class Orchestra < ActiveRecord::Base
has_many :performances
end
一个更有效的解决方案可能是首先避免从数据库中取出过期的性能
# in wherever your finder code lives
@performances = Orchestra.find(params[:id]).performances.where('date >= ?', Date.today)
您可以尝试以下操作(我想知道您是否可以在CollectionProxy对象上调用
.where
)性能。where('date>current\u date')
(current\u date
是PostgreSQL函数)您无法在CollectionProxy对象上调用where。我最终不得不将其转换为数组(这是我希望避免的)。谢谢你的建议!
# in wherever your finder code lives
@performances = Orchestra.find(params[:id]).performances.where('date >= ?', Date.today)