
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`)
);
class CreateForums < ActiveRecord::Migration
def self.up
create_table :authors do |t|
t.string :name
end
create_table :categories do |t|
t.string :name
end
create_table :forums do |t|
t.integer :category_id
t.string :name
t.text :description
t.timestamps
end
create_table :messages do |t|
t.string :type
t.integer :forum_id
t.integer :author_id
t.integer :topic_id
t.string :title
t.text :content
t.timestamps
end
end
def self.down
drop_table :authors
drop_table :categories
drop_table :forums
drop_table :messages
end
end
class Author < ActiveRecord::Base
has_many :messages
end
class Category < ActiveRecord::Base
has_many :forums
end
class Forum < ActiveRecord::Base
belongs_to :category
has_many :topics
end
class Message < ActiveRecord::Base
belongs_to :author
end
class Topic < Message
belongs_to :forum
has_many :replies
end
class Reply < Message
belongs_to :topic
end
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?
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
ActiveRecord makes these distinctions by using the Inflector, an integral part of ActiveSupport. — Julik
It’s also worth checking out the API documentation 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?
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 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.
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?
Yes, when records in a single table have and belong to other records in the same table you use acts_as_tree — AlexWayne
Where are the details for Author saved? There is no SQL Schema; was this an oversight?
It is not a prime consideration of this example to include a fully-working user system, so this table has been left out; however the only reference to it is by its id so this table could have any schema. — SamuelCochran
Surely this gets the usage for has_one wrong? Messages belong_to authors as the foreign key is in the messages table?
Yes, actually, it did. The code has been updated to reflect that. Messages surely do belong_to authors, as the messages table contains a foreign key to the authors table. The has_one relationship is only useful in a purely one-to-one relationship where the other object’s table contains the foreign key. In this case, if a Message has_one Author, then the Author table must contain a message_id, which means that an author may only have a single message. This is obviously incorrect. — Kevin Nardi
Let’s say I wanted to get all the categories a certain user posted in. How do I get that information withouth using a custom query through :finder_sql (and thus losing the find_in_collection ownage)? It would also be great to see how many post that author made in a category while I’m at it
category:Example

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`)
);
class CreateForums < ActiveRecord::Migration
def self.up
create_table :authors do |t|
t.string :name
end
create_table :categories do |t|
t.string :name
end
create_table :forums do |t|
t.integer :category_id
t.string :name
t.text :description
t.timestamps
end
create_table :messages do |t|
t.string :type
t.integer :forum_id
t.integer :author_id
t.integer :topic_id
t.string :title
t.text :content
t.timestamps
end
end
def self.down
drop_table :authors
drop_table :categories
drop_table :forums
drop_table :messages
end
end
class Author < ActiveRecord::Base
has_many :messages
end
class Category < ActiveRecord::Base
has_many :forums
end
class Forum < ActiveRecord::Base
belongs_to :category
has_many :topics
end
class Message < ActiveRecord::Base
belongs_to :author
end
class Topic < Message
belongs_to :forum
has_many :replies
end
class Reply < Message
belongs_to :topic
end
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?
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
ActiveRecord makes these distinctions by using the Inflector, an integral part of ActiveSupport. — Julik
It’s also worth checking out the API documentation 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?
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 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.
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?
Yes, when records in a single table have and belong to other records in the same table you use acts_as_tree — AlexWayne
Where are the details for Author saved? There is no SQL Schema; was this an oversight?
It is not a prime consideration of this example to include a fully-working user system, so this table has been left out; however the only reference to it is by its id so this table could have any schema. — SamuelCochran
Surely this gets the usage for has_one wrong? Messages belong_to authors as the foreign key is in the messages table?
Yes, actually, it did. The code has been updated to reflect that. Messages surely do belong_to authors, as the messages table contains a foreign key to the authors table. The has_one relationship is only useful in a purely one-to-one relationship where the other object’s table contains the foreign key. In this case, if a Message has_one Author, then the Author table must contain a message_id, which means that an author may only have a single message. This is obviously incorrect. — Kevin Nardi
Let’s say I wanted to get all the categories a certain user posted in. How do I get that information withouth using a custom query through :finder_sql (and thus losing the find_in_collection ownage)? It would also be great to see how many post that author made in a category while I’m at it
category:Example