Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Fixtures - what do these mean?

Context

When I generate the fixture file, rails generate model Post title:string text:text it shows:

one:
  title: MyString
  text: MyText

two:
  title: MyString
  text: MyText

Question

I was wondering why are there 2, and why are they named "one" and "two".

Here I see that names like "google", "rubyonrails", and "parent/child" are used; whereas, following the tutorial for generating the posts model, it generates just one and two...

Upon more research, I found that I might also be interested in db/migrate files. My current theory is that these files create the structure of my data... so I'm not so sure what fixtures does.

Reason

I'm trying to create a "Student" model using

rails generate scaffold student

but it doesn't seem to have :name as one of its keys. I'm looking into fixtures to add data columns.

like image 810
sihrc Avatar asked Aug 18 '13 23:08

sihrc


1 Answers

Just some quick notes on your question:

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

By default, Rails provides two fixtures named 'one' and 'two' every time. You can change them as you so please, Also, the data that goes into the fixtures is made when you pass in the keys for the database columns you want when using the generator. In the the first example were you used rails g model Post title:string... you created a model called Post and passed in two keys: :title and :text.

Answer:

As for your last question, you can quickly resolve the issue by a) Deleting the old scaffold by typing the following in your command line:

rails d scaffold Student

b) Creating it again but this time with the keys you want:

rails g scaffold Student name:string
like image 88
Ronathan Avatar answered Sep 22 '22 00:09

Ronathan