What does this mean in the Rail Tutorial:
Group (* Args) Specifying public group attributes Allows:
User.group (: name)
=> select "user". Based on the group group attribute named "by users", an array with different records:
User.group (: name) [user id: 3, name: " Foo ", ...>, # User ID: 2, name:" Oscars ", ... & gt;]
Do not group me with the example given by them Appears ...
I give a few examples.
1 If you want to know how many users are in your data base, you can do it with each name:
User.group (: name). Count
This will return a hash by looking at something like this:
{ann: 4, bert: 15, cecilia: 3 ...}
I do not know why there are so many bares in your database but anyway ...
2 If your users have related records (examples of cars), then you can call it your active record Joining the model Gali can use it to get the first car (this will be the first reason why the group works and then explained in the link below)
User.joins (: Cars) .select ('users. *, Car.model car_model, cars.name car_name') The group ('users.id')
Now all records of this result will be a method named car_model
and a car_name
.
-
You can count how many cars each user has with a single query.
User.joins (: cars). Select ('user. *, Count (car.id) as car_count'). Group ('users.id')
All records will now have car_count
.
For further reading:
Hope this group give sufficient amount of light to try a little for you. I do not think you can fully understand them unless you do a little bit of work with them.
No comments:
Post a Comment