I use example with Rails 3, but I believe that this is true for Rails 2.3 as well.
Suppose, I have model City that has many Locations. I try to find Cities that have locations.
I use following code:
City.joins(:locations)
But output array is:
=> [#<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">, #<City id: 5, name: "moscow", created_at: "2010-07-02 15:09:16", updated_at: "2010-07-02 15:09:16">]
Array length is 4 (number of locations of Moscow).
In what case it can be useful? For what aims are 4 copies of one object in output-array?
I can use City.joins(:locations).uniq, but I lost agile of arel.
I have two questions:
- Why joins returns non unique array?
- What is prefer to use instead of joins for this purpose?