github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

acunote / virtual_attributes

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 12
    • 0
  • Source
  • Commits
  • Network (0)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Preload associations when Rails can't — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Add MIT license file 
adymo (author)
Tue May 05 08:28:40 -0700 2009
commit  f24311189ad1868cd8a3de3c3c16dba33b8bf7d3
tree    345f1a55a9845d405e7de8ab8b78afe171dca2f2
parent  4c76c194defa42c9e1f6f82a13612363acbc0a2e
virtual_attributes /
name age
history
message
file LICENSE Loading commit data...
file README
file Rakefile
file init.rb
directory lib/
directory test/
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.
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server