我正在尝试计算一些活动(belongs_to学生模型),其中学生的性别(学生上的字符串)等于“男性”:
@boysout = Activity.where(status: "Out",
user_id: current_user,
student: {gender: "male" }).count
这是错误:
SQLite3 :: SQLException:没有这样的列:student.gender:SELECT COUNT(*) 从“活动”到“活动”。“状态”=?和 “活动”。“user_id”= 1和“学生”。“性别”=?
正确的语法是什么?
编辑:这是架构:
create_table "activities", force: :cascade do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "student_id"
t.string "status"
t.integer "user_id"
t.index ["student_id"], name: "index_activities_on_student_id"
t.index ["user_id"], name: "index_activities_on_user_id"
end
create_table "students", force: :cascade do |t|
t.string "firstname"
t.string "gender"
t.integer "grade"
t.string "school"
t.string "teacher"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
答案 0 :(得分:1)
更清洁的解决方案可以是这样:)。
假设student has_many activities
关联: -
Student.includes(:activities).where(gender:"male").where( :activities => { :status => "Out", user_id: current_user.id } ).count
答案 1 :(得分:0)
您是否尝试使用join
?
Activity.joins(:student)
.where(status: "Out", user_id: current_user.id)
.where(student: { gender: "male" })
.count