gpt4 book ai didi

ruby - Rails,防止 Model.scoped 的弃用警告,找到(:all) and relation #all?

转载 作者:数据小太阳 更新时间:2023-10-29 07:30:20 24 4
gpt4 key购买 nike

我有通过但显示的测试

$ rspec spec/event_calendar_spec.rb 
......DEPRECATION WARNING: Model.scoped is deprecated. Please use Model.all instead. (called from events_for_date_range at /home/durrantm/Dropbox/96_2013/work/code/ruby/event_calendar/lib/event_calendar.rb:52)
DEPRECATION WARNING: Calling #find(:all) is deprecated. Please call #all directly instead. You have also used finder options. These are also deprecated. Please build a scope instead of using finder options. (called from events_for_date_range at /home/durrantm/Dropbox/96_2013/work/code/ruby/event_calendar/lib/event_calendar.rb:52)
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a relation, you can call #load (e.g. `Post.where(published: true).load`). If you want to get an array of records from a relation, you can call #to_a (e.g. `Post.where(published: true).to_a`). (called from events_for_date_range at /home/durrantm/Dropbox/96_2013/work/code/ruby/event_calendar/lib/event_calendar.rb:52)

模型是:

 50     # Get the events overlapping the given start and end dates
51 def events_for_date_range(start_d, end_d, find_options = {})
52 self.scoped(find_options).find(
53 :all,
54 :conditions => [ "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ],
55 :order => "#{self.quoted_table_name}.#{self.start_at_field} ASC"
56 )
57 end

我尝试了几种删除 .scoped、删除 :all 和使用 where 但似乎都不起作用的方法,例如:

 50     # Get the events overlapping the given start and end dates
51 def events_for_date_range(start_d, end_d, find_options = {})
52 self.all.where(find_options,
53 :conditions => [ "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ],
54 :order => "#{self.quoted_table_name}.#{self.start_at_field} ASC"
55 )
56 end

给予:

  expected: [#<CustomEvent id: 1, custom_start_at: "2013-10-27 13:12:00", custom_end_at: "2013-10-29 13:12:00">]
got: #<ActiveRecord::Relation [#<CustomEvent id: 1, custom_start_at: "2013-10-27 17:12:00", custom_end_at: "2013-10-29 17:12

如何更新查找以不显示弃用警告?

最佳答案

all 是隐含的,使用范围时不需要它。

 50     # Get the events overlapping the given start and end dates
51 def events_for_date_range(start_d, end_d, find_options = {})
52 self.where(find_options,
53 :conditions => [ "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ],
54 :order => "#{self.quoted_table_name}.#{self.start_at_field} ASC"
55 )
56 end

如果您确实想要所有记录,则只能使用all:CustomEvent.all。如果您担心默认范围,您可以这样做:

 50     # Get the events overlapping the given start and end dates
51 def events_for_date_range(start_d, end_d, find_options = {})
52 self.unscoped.where(find_options,
53 :conditions => [ "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ],
54 :order => "#{self.quoted_table_name}.#{self.start_at_field} ASC"
55 )
56 end

如果您在 Rails 4 中使用 all 作为强制预先加载的方式,您现在应该使用 load :

 50     # Get the events overlapping the given start and end dates
51 def events_for_date_range(start_d, end_d, find_options = {})
52 self.where(find_options,
53 :conditions => [ "(? <= #{self.quoted_table_name}.#{self.end_at_field}) AND (#{self.quoted_table_name}.#{self.start_at_field}< ?)", start_d.to_time.utc, end_d.to_time.utc ],
54 :order => "#{self.quoted_table_name}.#{self.start_at_field} ASC"
55 ).load
56 end

关于ruby - Rails,防止 Model.scoped 的弃用警告,找到(:all) and relation #all?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19641025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com