【问题标题】:Displaying data within a date range from a rails scaffold从 Rails 脚手架显示日期范围内的数据
【发布时间】:2012-06-29 15:22:49
【问题描述】:

这是在我的控制器中:

@somethings = Something.find(:all)

我的计数变量将显示基于“rca”的数据分解:

@something_to_count = Something.count( :conditions => { :rca => "4X32W"})
@something_to_count2 = Something.count( :conditions => { :rca => "6X36W"})

.....等到其他计数变量...

这很简单,我试图让它在一个视图中显示过去三十天内从“某事”表中创建的所有条目的这些统计信息/计数,并在另一个视图中显示某个日期范围内的所有条目。我的模型中有脚手架函数(created_atupdated_at)添加的默认时间戳。

我的视图必须分成两页。一页接受日期范围并显示计数变量以及表格中的数据,另一页显示过去三十天的相同。 (即def last_30_daysdef within_dates 将是动作)。我在网上找到了一个解决方案,它从 30 等中减去了一个日期变量,这似乎不起作用。一个完整的工作解决方案将不胜感激。请帮忙。另外,如何将简单的日期范围条目转换为与日期戳相同的格式?

【问题讨论】:

    标签: mysql ruby-on-rails view controller scaffold


    【解决方案1】:

    您可以为查询创建范围,以便重复使用它们。
    我对您的问题的处理方法是这样的:

    scope :last_30_days, where("created_at between ? and ?", Date.today - 30, Date.today)
    scope :within_dates, lambda { |start_date, end_date|
      where("created_at between ? and ?", start_date, end_date)
    }
    scope :based_on_rca, lambda { |rca|
      where(:rca => rca)
    }
    
    @something_to_count = Something.based_on_rca("4X32W").last_30_days.count
    @something_to_count = Something.based_on_rca("4X32W").within_dates(Date.today - 4.month, Date.today - 1.month).count
    

    具有小查询的各种范围可以链接起来以反映更大的查询。 问候。

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      相关资源
      最近更新 更多