我有三个模型用户、活动和活动记录。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :email, :gender, :password, :password_confirmation, :remember_me
# attr_accessible :title, :body
has_many :activities
has_many :activity_records , :through=> :activities
end
class Activity < ActiveRecord::Base
attr_accessible :point, :title
belongs_to :user
has_many :activity_records
end
class ActivityRecord < ActiveRecord::Base
attr_accessible :activity_id
belongs_to :activity
scope :today, lambda { where("DATE(#{'activity_records'}.created_at) = '#{Date.today.to_s(:db)}'")}
end
我想查询用户的所有活动以及他们今天各自活动记录的计数。例如,查询并转换为 json 格式后,我想有类似下面的内容
[
{
id: 23
title: "jogging",
point: "5",
today_activity_records_count: 1,
},
{
id: 12
title: "diet dinner",
point: "2",
today_activity_records_count: 0,
},
]
请指导我如何实现这一目标。
谢谢