I've never done it, but my first idea would be to wrap up the master-detail "transformation" in the master objects CRUD methods and use the "has_many :children" and "belongs_to :parent" functionality.
<br><br>One concern is that you lose a lot of the power of using indexes to optimize your queries when implementing this type of database model. It may not matter in your case...<br><br><div><span class="gmail_quote">On 11/9/06,
<b class="gmail_sendername">Tim Pease</b> <<a href="mailto:tim.pease@gmail.com">tim.pease@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
This is a little long, but please bear with the explanation.<br><br>I want to be able to setup a database table that can hold different<br>kinds of "stuff" where each type of "stuff" can have vastly different
<br>contents (or attributes). For example, one type of stuff would be<br>information about a person -- name, age, address, hair color, etc.<br>Another type of stuff would be a book -- title, author, ISBN.<br><br>I would like to store all these things in one table so that I can have
<br>a join table linking a user to their "stuff". The join table would<br>have user specific attributes about the stuff (description, tags,<br>etc). The "stuff" table would only have one copy of each "stuff"
<br>item.<br><br>Some SQL ...<br><br>CREATE TABLE stuff (<br> id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,<br> type VARCHAR(50) NOT NULL,<br> name TEXT NOT NULL,<br> description TEXT NULL<br>);<br><br>CREATE TABLE stuff_attributes (
<br> stuff_id INT NOT NULL,<br> name VARCHAR(45) NOT NULL,<br> value TEXT NOT NULL<br>);<br><br><br>The idea here is that each "type" of stuff gets stored in the stuff<br>table with the type attribute set to what it is ('person' or 'book').
<br>The attributes about that stuff are then stored in the<br>stuff_attributes table, with each attribute being a separate entry<br>with the "name" of the attribute and the "value" for the attribute.<br>
<br>For a book, then, there would a single entry in the stuff table<br>describing the thing's type as a "book", the book's name and a<br>description. The stuff_attributes table would have three entries --<br>the title attribute and its value, the author attribute and its value,
<br>and the ISBN attribute and its value.<br><br>Obviously there will need to be an SQL join when doing any query to<br>get information about particular stuff.<br><br>Does ActiveRecord make this dead simple? Or will I end up rewriting
<br>most of the ActiveRecord methods to make this kind of table schema<br>work?<br><br>More general, is there a better way of doing this kind of generic data<br>about stuff in ActiveRecord?<br><br>Any helpful pointers are appreciated. Actual working Rails code with
<br>attached database schema will get you a free beer on Wednesday night<br>;)<br><br>Blessings,<br>TwP<br>_______________________________________________<br>Bdrg-members mailing list<br><a href="mailto:Bdrg-members@rubyforge.org">
Bdrg-members@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/bdrg-members">http://rubyforge.org/mailman/listinfo/bdrg-members</a><br></blockquote></div><br>