antonpug
antonpug

Reputation: 14296

Creating a very basic relational RoR app?

So let's say the problem is easy:

A books application where:

Books
--------------
Author ID, Title, Publisher, ISBN


Authors
--------------
Author ID, Last_Name, First_Name

I use generate model to create these objects. Further, I edit the app/models/books and app/models/authors files to add associations.

Would I only have to add one association to each file? has_one :authors in books and has_many :books in authors.

Also, where exactly are fixture files? To fill in with test data? They are not showing up in test/fixtures

Upvotes: 0

Views: 49

Answers (1)

user1976
user1976

Reputation:

Presumably you did something like the following:

> rails g model books author_id:integer title:string publisher:string isbn:string
  invoke  active_record
  create    db/migrate/20111203052638_create_books.rb
  create    app/models/books.rb
  invoke    test_unit
  create      test/unit/books_test.rb
  create      test/fixtures/books.yml
> rails g model author last_name:string first_name:string
  invoke  active_record
  create    db/migrate/20111203052732_create_authors.rb
  create    app/models/author.rb
  invoke    test_unit
  create      test/unit/author_test.rb
  create      test/fixtures/authors.yml

Fixtures should be generated along with the model in test/fixtures.

Your models should look something like this:

class Author < ActiveRecord::Base
  has_many :books
end

class Books < ActiveRecord::Base
  belongs_to :author
end

Upvotes: 1

Related Questions