Rails,activerecord查询与计算

时间:2014-12-30 06:20:27

标签: ruby-on-rails ruby activerecord report

我一直在努力解决这个问题,最后我决定寻求社区的帮助。

我正在尝试生成使用以下模型的报告。

class Volunteer < User
  has_one :volunteer_profile
  has_many :junior_leagues
  has_many :refresher_courses
  has_and_belongs_to_many :volunteer_programs, foreign_key: "user_id"
  accepts_nested_attributes_for :volunteer_profile
  validates :first_name, presence: true
  has_one :volunteer_profile
  before_validation :generate_password

  has_many :junior_leagues, foreign_key: "volunteer_id", dependent: :destroy
  has_many :refresher_courses, foreign_key: "volunteer_id", dependent: :destroy
  has_many :users_mentors, class_name: 'UsersMentor', foreign_key: 'mentor_id'
  has_many :mentees, through: :users_mentors, class_name: 'User', foreign_key: :user_id
  has_many :volunteer_schedules, dependent: :destroy
  has_many :programs, through: :volunteer_schedules

  accepts_nested_attributes_for :volunteer_profile
  accepts_nested_attributes_for :users_mentors

这是一个志愿者课程,这是一个志愿者时间表:

  class VolunteerSchedule < ActiveRecord::Base
  attr_accessor :program_type_name

  ROLE = { lead_volunteer: 1, shadowing: 2 }

  validates :end_time, :start_time, :volunteer_id, presence: true
  validates :volunteer_id, uniqueness: {scope: [:start_time, :end_time, :program_type_id],     message: "Already added."}

  belongs_to :program
  belongs_to :program_type
  belongs_to :volunteer
  belongs_to :volunteer_program
  # VolunteerProgram

  before_validation :set_program_type

  ##SCOPES##
  scope :where_date, -> (date) { where('DATE(start_time) = ?', date) }
  scope :personal_shoppers, -> { where(program_type_id: ProgramType.find_by(name:    ProgramType::PERSONAL_SHOPPERS).try(:id)) }

我正在尝试运行一个查询,这将允许我根据日期范围获取志愿者列表和每个志愿者的总工作时间。

这是我到目前为止的控制器方法:

def volunteer_hours
@volunteer_names = Array.new

@volunteer_schedule_times = VolunteerSchedule.joins(:volunteer).where( start_time: params[:vol_start_time]..params[:vol_end_time] )

@volunteer_schedule_times.each do |current_schedule_times|
   @volunteer_names << current_schedule_times.volunteer.name
 end

@volunteer_names.uniq!

hours_for_each_volunteer = @volunteer_names.each do |volunteer|
  VolunteerSchedule.joins(:volunteer).where(start_time: params[:vol_start_time], name: volunteer)
 end

我必须分组,group_by,我写了一堆辅助方法,但很快就搞砸了,并且遇到了一堆范围问题。我需要帮助这个和

如果情况有用,请快速查看架构:

  create_table "volunteer_schedules", force: true do |t|
t.datetime "start_time"
t.datetime "end_time"
t.integer  "program_id"
t.integer  "volunteer_id"
t.integer  "role"
t.datetime "created_at"
t.datetime "updated_at"
t.integer  "program_type_id"
t.integer  "volunteer_program_id"
end

志愿者继承用户:

  create_table "users", force: true do |t|
t.string   "email"
t.string   "encrypted_password",                 default: "",   null: false
t.string   "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer  "sign_in_count",                      default: 0,    null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string   "current_sign_in_ip"
t.string   "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string   "avatar"
t.string   "phone"
t.boolean  "phone_is_cell",                      default: true
t.string   "second_alternate_phone"
t.boolean  "second_alternate_phone_is_cell",     default: true
t.string   "type"
t.string   "first_name"
t.string   "middle_initial"
t.string   "last_name"
t.date     "date_of_birth"
t.boolean  "veteran"
t.string   "street_address"
t.string   "apartment_unit"
t.string   "city"
t.string   "state"
t.string   "zip"
t.boolean  "receive_appointment_text_reminders", default: true
t.boolean  "alternate_phone"
t.boolean  "alternate_phone_is_cell",            default: true
t.boolean  "active",                             default: true
t.boolean  "star_client"
t.float    "gpa"
end

我可以对此使用一些严肃的帮助。

0 个答案:

没有答案