[Rails] A.R. Associations problem

richard downe
Fri, 31 Mar 2006 00:32:04 -0000

Hello,

I''m learning A.R Associations by creating a simple forum that consists
of 3 tables: ahuthors, topics and posts.  This is the schema:
  class AddAuthorAndTopicAndPostTables < ActiveRecord::Migration
  def self.up
    create_table :authors do |t|
      t.column :username, :string
      t.column :email, :string
      t.column :created_on, :datetime
    end

    create_table :topics do |t|
      t.column :topic, :string
      t.column :author_id, :integer
      t.column :created_on, :datetime
    end

    create_table :posts do |t|
      t.column :post, :text
      t.column :topic_id, :integer
      t.column :author_id, :integer
      t.column :created_on, :datetime
    end
  end

  def self.down
    drop_table :authors
    drop_table :topics
    drop_table :posts
  end
end

Here are the model associations:
class Author < ActiveRecord::Base
  has_many :topics
  has_many :posts
end

class Topic < ActiveRecord::Base
  has_many :posts
  belongs_to :author
end

--
Posted via http://www.ruby-forum.com/.