https://github.com/ledermann/redundant_links
Automatic handling of a join table for indirect (transitive) associations.
https://github.com/ledermann/redundant_links
Last synced: over 1 year ago
JSON representation
Automatic handling of a join table for indirect (transitive) associations.
- Host: GitHub
- URL: https://github.com/ledermann/redundant_links
- Owner: ledermann
- License: mit
- Created: 2008-10-29T16:31:14.000Z (over 17 years ago)
- Default Branch: master
- Last Pushed: 2011-04-10T06:05:08.000Z (about 15 years ago)
- Last Synced: 2025-02-15T18:54:22.354Z (over 1 year ago)
- Language: Ruby
- Homepage:
- Size: 113 KB
- Stars: 2
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README
- License: MIT-LICENSE
Awesome Lists containing this project
README
redundant_links
===============
Automatic handling of a join table for indirect associations
Plugin for Ruby on Rails 2.1 (or newer)
Example
=======
Assume you have contacts who can place multiple orders. In addition, contacts
and orders may have multiple notes. This plugin helps you to get the notes
of a contact, even if they belong to one of its orders. This is just a simple
example. Of course it works in more complex situations, too.
class Contact < ActiveRecord::Base
has_many :orders
has_many :notes, :as => :object
end
class Order < ActiveRecord::Base
belongs_to :contact
has_many :notes, :as => :object
end
class Note < ActiveRecord::Base
belongs_to :object, :polymorphic => true
has_redundant_links Note => :object,
Order => :contact,
Contact => nil
end
contact = Contact.find(123)
Now you can do:
With plain Rails magic:
contact.notes
=> Gives you only the notes which directly belong to the contact
With this plugin:
contact.redundant_linked_notes
=> Gives you the notes which either directly belong to the contact or
belong to orders of this contact
How it works
============
The plugin manages a join table named "redundant_links" with all direct and
indirect associations between a detail model and defined master models. It
creates an instance method in all of the master models to get the linked
detail records (based on a simple INNER JOIN, so it's quite fast).
Installation
============
script/plugin install git://github.com/ledermann/redundant_links.git
script/generate redundant_link_migration add_redundant_links_table
rake db:migrate
Usage
=====
In your detail model place this
has_redundant_links(options)
See the source code for more information about the options hash.
ToDo
====
- Some optimizations, so updating join table records is only made if it's needed
Copyright (c) 2008 Georg Ledermann, released under the MIT license