行,所以我有这个帮手有没有更好的方式来做到的红宝石这个发现
def playlist_count(user, site_id)
user.companies.local(site_id).map(&:playlists).flatten.count
end
将返回播放列表计数所有公司
class Playlist < ActiveRecord::Base
belongs_to :company
scope :active, where('end_date >= ? AND player_id IS NOT NULL', Date.today)
class Company < ActiveRecord::Base
has_many :playlists, :dependent => :destroy
scope :local, lambda{ |site_id| where(:site_id => site_id) }
的问题是,它的帮手越来越丑陋,另一个问题是我需要活动播放列表(由我的范围在播放列表模型中定义)
有没有办法清理我的帮手或使用范围来取得活动的播放列表计数对于所有用户公司
使Date.today评估的任何时间范围是叫你应该换你的活跃范围的说法在拉姆达。 'scope:active,lambda {where(...)}' –