!http://media.nextangle.com/rails/forum.gif! h2. Database schema (\MySQL)
CREATE TABLE 'categories' (
'id' int(11) NOT NULL auto_increment,
'name' varchar(50) DEFAULT NULL,
PRIMARY KEY ('id')
);
CREATE TABLE 'forums' (
'id' int(11) NOT NULL auto_increment,
'category_id' int(11) DEFAULT NULL,
'name' varchar(50) DEFAULT NULL,
'description' varchar(100) DEFAULT NULL,
PRIMARY KEY ('id')
);
CREATE TABLE 'messages' (
'id' int(11) NOT NULL auto_increment,
'type' varchar(50) DEFAULT NULL,
'forum_id' int(11) DEFAULT NULL,
'author_id' int(11) DEFAULT NULL,
'topic_id' int(11) DEFAULT NULL,
'title' varchar(255) DEFAULT NULL,
'content' text,
'written_on' datetime DEFAULT NULL,
PRIMARY KEY ('id')
);
h2. Model code
class User < \ActiveRecord::Base
has_many :replies
end
class Category < \ActiveRecord::Base
has_many :forums
end
class Forum < \ActiveRecord::Base
belongs_to :category
has_many :topics
end
class Message < \ActiveRecord::Base
has_one :author
end
class Topic < Message
belongs_to :forum
has_many :replies
end
class Reply < Message
belongs_to :topic
end
h2. Questions
How does \ActiveRecord know that "replies" maps to the class "Reply"? How does this work when the mapping is lowercase and plural and the class name is uppercase and singular?
bq. Apparently Active Record knows how to convert singular to plural (for the most part — it still doesn't properly work with irregular noun endings... but no big deal). It also knows how to convert bumpy text in class names, such as \ActiveRecord, into lowercase equivalents, like active_record. — CarlYoungblood
bq. It's also worth checking out the "API documentation":http://ar.rubyonrails.org/ for general information on how the binding between database table and Ruby class is achieved. You can specify the table in the code if it won't resolve automatically (e.g. @Person@ -> @people@).
*What is this example trying to prove? What are the "virtual" tables? Can you make available an SQLite database file with these tables and some sample data so we can play with the code and see what it does?*
bq. LukeHolden: The rails way to handle [[Inheritance]] in the database is through what I like to call a 'virtual' table. If you look at the SQL above, you will see that there is a table named 'messages'. Topics and Replies, inherit 'messages' to [[AbstractModel|abstract]] the two different kinds of messages from each other. This allows you to query all of the 'topics', separately from the 'replies' even though they have the same structure. A 'virtual' table’s type is specified in the 'type' column. Expect examples of this in the \ActiveRecord release archive.
*So how do you indicate which properties each subclass inherits? For instance, say you have a Shape class with two subclasses, Circle and Square. To implement this model in Active Record, you'd create a single table, shapes, in which both circles and squares would live. (Circles would have "circle" in their type field, and Squares would have "square".) In that table, say you had a circumference field for circles and a length field for squares. How does Active Record know to only give square objects the length property and only circle objects the circumference property? It seems like all children classes inherit all the properties of not just their parents, but also of their siblings.*
bq. Yes. Both Circles and Squares would have the same properties. Just ignore the ones you don't need, or add to your model code to prevent them being accessed.
*To implement slashdot-style threaded replies, would TreeToTableMappingWish make sense?*
bq. Yes, when records in a single table have and belong to other records in the same table you use "acts_as_tree":http://rails.rubyonrails.com/classes/ActiveRecord/Acts/Tree/ClassMethods.html -- AlexWayne
Question: Where are the details for Author saved? There is no SQL Schema; was this an oversight?
Surely this gets the usage for has_one wrong? Messages belong_to authors as the foreign key is in the messages table?
category:Example