So I am designing a railsapp that indexes school organizations such as clubs and frats/sororities. I have decided to represent this as a graph, and that each entity is going to be just represented as a node, and the relationship between each node is going to be an edge. so basically, a frat such as "Alpha Beta Gamma" is represented as a node, and its chapters also as seperate nodes, and pledge classes as seperate nodes, then finally each member as a node. That''s easily done. Now the hard part. I want each node to be able to be "tagged" with a user specified attribute. this works out if I keep the attributes for the tags one type. but i want different types such as :string, :text, :date, :datetime, :boolean, etc for each. for example, the root node of "Alpha Beta Gamma" would have the attributes "established" with type :date, "history" with type :text, etc, while an individual would have "firstname" and "lastname" as string, "birthdate" as :date, etc. There are two ways to represent this... i can do a polymorphic associations to each type, such that i have an AttributeString model, AttributeDate model, etc. which belongs to the Node model polymorphically. Or what i can do is put all the Attributes in one model, with a column in the database for every kind of type: create_table :attributes do |t| t.column :name, :string t.column :type, :string t.column :attr_string, :string t.column :attr_text, :text t.column :attr_integer, :integer . . . t.column :attr_boolean, :boolean end so to access the attribute would be through a function that does a switch against the type. now, which way would be better? the polymorphic way, or the single table way? -- Posted via http://www.ruby-forum.com/.
J.r. Gutierrez wrote:> So I am designing a railsapp that indexes school organizations such as > clubs and frats/sororities. I have decided to represent this as a graph, > and that each entity is going to be just represented as a node, and the > relationship between each node is going to be an edge. > > so basically, a frat such as "Alpha Beta Gamma" is represented as a > node, and its chapters also as seperate nodes, and pledge classes as > seperate nodes, then finally each member as a node. That''s easily done. > > Now the hard part. I want each node to be able to be "tagged" with a > user specified attribute. this works out if I keep the attributes for > the tags one type. but i want different types such as :string, :text, > :date, :datetime, :boolean, etc for each. > > for example, the root node of "Alpha Beta Gamma" would have the > attributes "established" with type :date, "history" with type :text, > etc, while an individual would have "firstname" and "lastname" as > string, "birthdate" as :date, etc. > > There are two ways to represent this... i can do a polymorphic > associations to each type, such that i have an AttributeString model, > AttributeDate model, etc. which belongs to the Node model > polymorphically. Or what i can do is put all the Attributes in one > model, with a column in the database for every kind of type: > create_table :attributes do |t| > t.column :name, :string > t.column :type, :string > t.column :attr_string, :string > t.column :attr_text, :text > t.column :attr_integer, :integer > . > . > . > > t.column :attr_boolean, :boolean > end > > so to access the attribute would be through a function that does a > switch against the type. > > now, which way would be better? the polymorphic way, or the single table > way? >There''s a third way which might be easier. You can store the data in YAML format. You can then use .to_yaml and YAML::load to set/get your data. You''d only need a database table with two columns in that case the key and the value. A good example of this is the Settings plugin at: http://beautifulpixel.textdriven.com/articles/2006/02/25/settings-plugin take a look in lib/settings.rb. Hope that helps. dan