I have two tables.
StudentGroup and StudentGroupsMembers.
StudentGroup.id = StudentGroupMembers.group_id is the foreign key relation.
sg = StudentGroup.joins(:student_group_members).select(" student_groups.id, student_groups.name, count(student_group_members.id) as cnt ").group('student_group_members.group_id, student_groups.name, student_groups.id').where(id: [3,4,5,6,6,7,7,8,8]).where("student_group_members.member_type='ra'")
This return ActiveRecord_Relation
[#<StudentGroup:0x00007fc11253d088 id: 3, name: "Blah">,#<StudentGroup:0x00007fc11253cc78 id: 4, name: "Foo">,
#<StudentGroup:0x00007fc11253c7c8 id: 7, name: "Bar">,
#<StudentGroup:0x00007fc11253c390 id: 8, name: "ABc">]
But only when I do
sg[0]["cnt"] I’m able to get cnt value.
- Why?
- What should I do to get cnt as part of each record, i.e, not doing sg[0]["cnt"]?
2
Answers
Rails returns an ActiveRecord Association as you have noticed. You can use that as if it were a normal record with the joined columns becoming methods like the regular StudentGroup columns.
The association is returning an array of StudentGroup objects but they have the joined columns already queried, you just don’t see them in the StudentGroup object until you call the method. If you want you can append
pluck("student_groups.id, student_groups.name, cnt")
to the end of the query:You will get an array of arrays, each one containing the data you queried. But it is JUST the data from the columns, not a StudentGroup object. So you won’t be able to use all the StudentGroup methods on them…
If you use
sg
in some sort of rails view or method you can iterate over it just like a normal query of StudentGroup, but you will have the joined columns available as methods:Try this: