Hi Railers, A long, long, time ago, i made a Framework/CMS in php. There were two core models: Content and Content_type. Content_type was only a list of content types (as expected): new, article, etc. Content_type was not mandatory, but it allowed later to "deactivate" types of contents, for example. So then Content stored the common attributes/properties between the different content types (creation date, owner_id, etc). For each content type, there was a model i.e: Content_type_new Content_type_article ... Rails speaking, Content has_one Content_type_new, has_one Content_type_article, etc. So for each Article, there was a record in ''content'' table and another in ''content_type_article''. This approach makes more sense to me than having a unique huge table of contents, since i was able to code ONE page that handled each request of view, list or modification, regardless of the content type. I admit that on my way there are double amount of rows, instead of one record for each content but all is very cleaner and from the "model point of view": Content_type_new inherits from Content, allowing to use its methods, for example, among other common properties, as said above. Another approach would be to have different tables for each content type, but no ''content'' table. Taking into account, that all them are contents (a entity itself in my opinion), my Framework method seems more reasonable. In some cases, you can search only in ''content'' table, for example to know how many articles a user has, is faster, since this table does not contain any additional data as the articles themselves. Now, the real question: Here at my job, I''ve convinced my boss to rail the next project. Among a lot of other things, there will be a diary. It will have appointments, meetings, tasks, and, who knows, more things. I have named all those things ''events'' (note that the diary elements inherit from event, as articles did from content). So, ¿how would you do this database schema? I have a partner that says that entity-relation models simplify my multi-table approach to a single ''events'' or ''contents'' table. Thanks for reading, and sorry if my English is not as good as expected, Rodrigo.
On Oct 19, 2005, at 10:43 AM, Rodrigo Alvarez Fernández wrote:> Now, the real question: > Here at my job, I''ve convinced my boss to rail the next project. Among > a lot of other things, there will be a diary. It will have > appointments, meetings, tasks, and, who knows, more things. > I have named all those things ''events'' (note that the diary elements > inherit from event, as articles did from content). So, ¿how would you > do this database schema? > I have a partner that says that entity-relation models simplify my > multi-table approach to a single ''events'' or ''contents'' table.class Content < ActiveRecord::Base end class Appointment < Content end class Task < Content end class Note < Content end class Diary < ActiveRecord:: Base has_many :appointemtns has_many :tasks has_many :notes end Your content table will have a type varchar(255) column. You''ll also add all the columns for all the different types of content to the content table. All the content will share the same table. So a Note might have a body column. That would exist in the content table. There would be no notes table. All this assumes that you''ll never need to change a Note into an Appointment or anything similar. Single Table Inheritance in Rails doesn''t allow for re-typing objects. There''s a little documentation on this in the API for ActiveRecord::Base, http://api.rubyonrails.com/classes/ActiveRecord/ Base.html. I was disappointed to not find any pages in the wiki about STI. I guess that means I should write something... ----- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On Oct 19, 2005, at 9:57 AM, Doug Alcorn wrote:> All this assumes that you''ll never need to change a Note into an > Appointment or anything similar. Single Table Inheritance in Rails > doesn''t allow for re-typing objects.Actually, it does... note.update_attribute :type, "Appointment" appointment = Appointment.find(note.id)> There''s a little documentation on this in the API for > ActiveRecord::Base, http://api.rubyonrails.com/classes/ActiveRecord/ > Base.html. I was disappointed to not find any pages in the wiki > about STI. I guess that means I should write something...Please do! - Jamis
On Oct 19, 2005, at 11:08 AM, Jamis Buck wrote:> On Oct 19, 2005, at 9:57 AM, Doug Alcorn wrote: > > >> All this assumes that you''ll never need to change a Note into an >> Appointment or anything similar. Single Table Inheritance in >> Rails doesn''t allow for re-typing objects. >> > > Actually, it does... > > note.update_attribute :type, "Appointment" > appointment = Appointment.find(note.id)Sweet! I didn''t realize that would work.> >> There''s a little documentation on this in the API for >> ActiveRecord::Base, http://api.rubyonrails.com/classes/ >> ActiveRecord/Base.html. I was disappointed to not find any pages >> in the wiki about STI. I guess that means I should write >> something... >> > > Please do!<excuse type="lame">Maybe I''m not the best one to do it....</excuse>
On 10/19/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> > On Oct 19, 2005, at 10:43 AM, Rodrigo Alvarez Fernández wrote: > > > class Content < ActiveRecord::Base > end > > class Appointment < Content > end > > class Task < Content > end > > class Note < Content > end > > class Diary < ActiveRecord:: Base > has_many :appointemtns > has_many :tasks > has_many :notes > end > > Your content table will have a type varchar(255) column. You''ll also > add all the columns for all the different types of content to the > content table. All the content will share the same table. So a Note > might have a body column. That would exist in the content table. > There would be no notes table. > > All this assumes that you''ll never need to change a Note into an > Appointment or anything similar. Single Table Inheritance in Rails > doesn''t allow for re-typing objects. > > There''s a little documentation on this in the API for > ActiveRecord::Base, http://api.rubyonrails.com/classes/ActiveRecord/ > Base.html. I was disappointed to not find any pages in the wiki > about STI. I guess that means I should write something... >It seems that scaffolding is not possible with this, is it? Apart from that, if you have to i18n the contents of your website, should you use the single table inheritance? (title_en, title_de, title_es...). I really don''t like this way... but it''s the rails way, so it should be ok ;/ Thanks for the info, i''ll give it a try.