acunote / virtual_attributes

Preload associations when Rails can't

This URL has Read+Write access

name age message
file LICENSE Tue May 05 08:28:40 -0700 2009 Add MIT license file [adymo]
file README Tue May 05 08:23:09 -0700 2009 Fix readme [adymo]
file Rakefile Tue May 05 08:08:39 -0700 2009 Initial import of Virtual Attributes plugin - t... [adymo]
file init.rb Tue May 05 08:08:39 -0700 2009 Initial import of Virtual Attributes plugin - t... [adymo]
directory lib/ Tue May 05 08:08:39 -0700 2009 Initial import of Virtual Attributes plugin - t... [adymo]
directory test/ Tue May 05 08:08:39 -0700 2009 Initial import of Virtual Attributes plugin - t... [adymo]
README
Virtual attributes plugin lets you do two things:
1) define "virtual" attributes that work as usual AR attributes,
   but that are not saveable to the database
2) define preloadable attributes and associations that take field values from
   database queries and prepopulate virtual attributes or associations
   without the need of extra database query.

For example, you have two associated tables Foo and Bar:

    class Bar < ActiveRecord::Base
    end

    class Foo < ActiveRecord::Base
        belongs_to :bar

        preloadable_association :bar
        preloadable_attribute :zee
    end

    b = Bar.create :name => 'Bar'
    f = Foo.create :name => 'Foo', :bar_id => b.id


With preloadable_attribute you can use any value from the SQL query as
attribute (just like with AR attribute).

With preloadable association you can preload associated model from
SQL query results without extra database query.
The only thing you must do is to select all fields from associated model
in the query, prefixed with "preloaded_<association_name>_".

Example:

    f = Foo.find_by_sql("select *, '1' as zee from foos left outer join (select id as preloaded_bar_id, name as 
    preloaded_bar_name from bars) as bars on foos.bar_id = bars.preloaded_bar_id").first

    assert_equal "Bar", f.bar.name
    assert_equal "1", f.zee


Copyright (c) 2008-2009 Pluron, Inc. Released under the MIT license.