Question

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.

Was it helpful?

Solution

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

OTHER TIPS

I'll start by saying that the code generated by the rails generate command is intended to be a starting point for parts of your application, helping you get going quickly.

That said - fixtures are intended for use in unit tests. They give you a way to generate a set of objects already existing in the system, so you don't have to create them by hand at the beginning of your test.

In this case, one and two are just placeholders. In a real app, you'd replace them with names that were more meaningful in your tests.

If you're looking to add columns of data in your app, fixtures probably aren't the right approach. They're really meant for use in tests, nothing more.

Try this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top