jcoglan / include_by_default

Specifies that associations should be included automatically with find() calls in ActiveRecord

This URL has Read+Write access

563feea7 » jcoglan 2007-07-09 Added README text. 1 == IncludeByDefault
edb71504 » jcoglan 2007-07-09 Imported new plugin. 2
563feea7 » jcoglan 2007-07-09 Added README text. 3 This plugin allows you to specify which associations should be eager-loaded automatically when you do a +find+ on one of your models. Read about eager loading in the Rails docs: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html
4
5 This saves you having to write <tt>:include</tt> all over the place to minimise your database queries. Instead, you can put whatever you'd usually specify using <tt>:include</tt> in one declaration in your model. Say you have a photoblog that allows comments. Then your entry model might look like this:
6
7 class BlogEntry << ActiveRecord::Base
8 has_many :photos
9 has_many :comments
10 include_by_default :photos, :comments
11 end
12
13 The associations will now be loaded automatically along with any <tt>BlogEntry</tt> record you +find+. If you specify <tt>:include</tt> explicitly in any +find+ operation, that will override the default specified in the model.
14
15
b34327e6 » jcoglan 2007-07-10 Added fix for duplicate tab... 16 == Bugs
17
18 As well as the functionality stated above, IncludeByDefault attempts to fix a couple of bugs in ActiveRecord:
19
20 * http://dev.rubyonrails.org/ticket/8838
21 * http://dev.rubyonrails.org/ticket/8937
22
23 The first is a problem with cascaded many-to-many queries. e.g.
24
25 Tag.find(8).entries.find(:all, :include => :tags)
26
efddb553 » jcoglan 2007-07-13 Greatly simplified workarou... 27 will usually raise an exception. IncludeByDefault fixes this by overloading <tt>ActiveRecord::Base.add_joins!</tt> so that it re-aliases any duplicate table names to disambiguate the SQL generated.
b34327e6 » jcoglan 2007-07-10 Added fix for duplicate tab... 28
29 The second is a problem that stops you using the <tt>:joins</tt> option on scoped many-to-many +find+ operations. That is, you cannot do
30
31 Tag.find(8).entries.find(:all, :joins => sql_fragment)
32
efddb553 » jcoglan 2007-07-13 Greatly simplified workarou... 33 if +Tag+ and +Entry+ are many-to-many. My rewrite of <tt>ActiveRecord::Base.add_joins!</tt> allows you do this. The rewrite does the table aliasing on the existing SQL before appending the new JOIN fragment, so the table names you use for <tt>:joins</tt> should be retained.
b34327e6 » jcoglan 2007-07-10 Added fix for duplicate tab... 34
35
563feea7 » jcoglan 2007-07-09 Added README text. 36 === License
37
38 Copyright (c) 2007 James Coglan
39
40 Permission is hereby granted, free of charge, to any person obtaining
41 a copy of this software and associated documentation files (the "Software"),
42 to deal in the Software without restriction, including without limitation
43 the rights to use, copy, modify, merge, publish, distribute, sublicense,
44 and/or sell copies of the Software, and to permit persons to whom the
45 Software is furnished to do so, subject to the following conditions:
46
47 The above copyright notice and this permission notice shall be included
48 in all copies or substantial portions of the Software.
49
50 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
51 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
52 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
53 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
54 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
55 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
56 DEALINGS IN THE SOFTWARE.