Seems like this is just a reverse of a normal polymorphic association. A
tricky problem to solve, since rails doesn't seem to be designed to handle
it.
When you call "school_instance.assets" the underlying query would
essentially need to be "give me everything from the books table, teachers
table, and chairs table". That's basically impossible if you understand the
SQL behind it, since each of these tables has a different schema. Single
Table Inheritance solves that issue.
It is possible to work around it if you want 3 separate tables...
If an asset can belong to only one school, it's probably easiest to do away
with the abstract parent class and just have them each "belong_to :school"
Then in your School class, you can add a method to retrieve all the
associated assets:
class School
has_many :teachers
has_many :books
has_many :chairs
def assets
self.teachers + self.books + self.chairs
end
end
To add an asset, you'll have to call the specific "teachers <<" "books <<"
or "chairs <<" However, you could also add a method to handle that as well:
def add_asset(item)
self.books << item if item.is_a?(Book)
self.chairs << item if item.is_a?(Chair)
self.teachers << item if item.is_a?(Teacher)
end
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.