Created
June 16, 2011 01:21
-
-
Save iamjarvo/1028506 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateBooks < ActiveRecord::Migration | |
def self.up | |
create_table :books do |t| | |
t.string :title, :null => false | |
t.text :description | |
t.string :isbn | |
t.belongs_to :authorship | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :books | |
end | |
end | |
class CreateAuthors < ActiveRecord::Migration | |
def self.up | |
create_table :authors do |t| | |
t.string :name, :null => false | |
t.string :about_author | |
t.belongs_to :authorship | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :authors | |
end | |
end | |
class CreateAuthorships < ActiveRecord::Migration | |
def self.up | |
create_table :authorships do |t| | |
t.integer :author_id, :null => false | |
t.integer :book_id, :null => false | |
t.timestamps | |
end | |
end | |
def self.down | |
drop_table :authorships | |
end | |
end | |
#models | |
class Author < ActiveRecord::Base | |
has_many :authorships | |
has_many :books, :through => :authorships | |
end | |
class Book < ActiveRecord::Base | |
has_many :authorships | |
has_many :authors, :through => :authorships | |
end | |
class Authorship < ActiveRecord::Base | |
belongs_to :author | |
belongs_to :book | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment