2012-12-12 118 views
2

查找记录有日期时间3天,我有以下范围:的Rails 3:从今天

def coming_up_on_renewal(product_name = "product_name") 
    where('billing_start_date = NOW() + INTERVAL 3 day AND status = ? AND product_name = ? AND auth_net_subscription_id IS NOT NULL', :active, "#{product_name}") 
end 

而且我已经在我的数据库操作的一条记录将在未来3天,但是当我运行这个范围,返回一个空数组。

我基本上试图检索billing_start_date是从当天起3天的记录。

回答

4

试试这个。

def coming_up_on_renewal(product_name) 
    where(:status => :active, :product_name => product_name) 
    .where(:billing_start_date => 
    (3.days.from_now.beginning_of_day)..(3.days.from_now.end_of_day)) 
    .where('auth_net_subscription_id IS NOT NULL') 
end 

对于您的额外“IS NOT NULL”条件,请参阅this answer

+0

运行您的activerecord语句时出现错误:'syntax error,unexpected',',expect tASSOC ... name,'billing_start_date =?',3.days.from_now)' – dennismonsewicz

+0

感谢您的帮助!我能够得到它:'where(:status =>:active,:product_name => product_name,:billing_start_date =>(Time.now.midnight + 3.day)..(Time.now.end_of_day + ('auth_net_subscription_id IS NOT NULL')' – dennismonsewicz

+1

我已经编辑了我的答案,但我不满意'billing_start_date'部分。使用'3.days.from_now',它正在查找具有完全时间戳的记录,直到第二个。你可能会看更多的日期范围。 –