Reputation: 451
I'm trying to do a count of activities (belongs_to Students model) where the students gender (string on student) is equal to "male":
@boysout = Activity.where(status: "Out",
user_id: current_user,
student: {gender: "male" }).count
This is the error:
SQLite3::SQLException: no such column: student.gender: SELECT COUNT(*) FROM "activities" WHERE "activities"."status" = ? AND "activities"."user_id" = 1 AND "student"."gender" = ?
What is the correct syntax?
Edit: Here's the schema:
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"
Upvotes: 1
Views: 136
Reputation: 5112
More cleaner solution can be this :).
Assuming student has_many activities
associations:-
Student.includes(:activities).where(gender:"male").where( :activities => { :status => "Out", user_id: current_user.id } ).count
Upvotes: 2
Reputation: 12402
Have you tried with a join
?
Activity.joins(:student)
.where(status: "Out", user_id: current_user.id)
.where(student: { gender: "male" })
.count
Upvotes: 0