0

我正在构建一个具有以下架构的广告管理器:

<Campaign id: nil, target_id: nil, date_view_based: nil, start_date: nil, end_date: nil, max_views: nil, cur_views: nil, link_to: nil, alt_data: nil, title_attr: nil, created_at: nil, updated_at: nil, user_id: nil, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, link_clicked: nil, name: nil>

我需要的是一种方法来创建一个像这样运行的方法......

Campaigns.current?

并输出这样的数组

Campaign.where('date_view_based =? AND end_date >= ? AND start_date <= ?', "Date", Date.today.strftime('%m/%d/%Y'), Date.today.strftime('%m/%d/%Y')).concat(Campaign.where('date_view_based = ? AND max_views >= ?, "View", :cur_views))

但是,Campaign.where('max_views >= ?, :cur_views)不起作用,那么我该如何比较这两个属性呢?当然,必须有更好的方法来做到这一点,任何建议都会受到赞赏。

抱歉,我以为我已经包含了结构,但我想我没有:Campaign.new返回:

#<Campaign id: nil, target_id: nil, date_view_based: nil, start_date: nil, end_date: nil, max_views: nil, cur_views: nil, created_at: nil, updated_at: nil, name: nil>
4

1 回答 1

1

这个怎么样?

class Campaign < ActiveRecord::Base

  # ...

  def self.current
    date_view_based_enum = %W(Date View)
    date = Date.today.strftime('%m/%d/%Y')
    where("((start_date <= ? AND end_date >= ?) OR (max_views >= cur_views)) AND date_view_based IN (?)", date, date, date_view_based_enum)
  end
end
于 2013-07-01T22:11:57.637 回答