Rails Fixtures - what does it mean?

Context

When I create the binding 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 there are 2 of them, and why they are called "one" and "two."

Here I see that names like "google", "rubyonrails" and "parent / child" are used; whereas after the tutorial on creating a message model, it generates only one and two ...

After more research, I found that db / migrate files might also be of interest to me. My current theory is that these files create the structure of my data ... so I'm not sure what the lights do.

Cause

I am trying to create a Student model using

 rails generate scaffold student 

but he does not seem to have: a name as one of his keys. I am looking for tools to add data columns.

+4
source share
3 answers

A few quick notes on your subject:

Lamps are data that you can submit for testing your device. They are automatically created when the rails generate the appropriate tests for your controllers and models. They are used only for your tests and, in fact, may not be available at application startup.

By default, Rails provides two fixtures named "one" and "two" each time. You can change them as you wish. In addition, the data entering the fixtures is executed when you pass the keys to the database columns that you want to use when using the generator. In the first example, you used rails g model Post title: string ... you created the Post model and passed two keys :: title and: text.

Answer:

As for your last question, you can quickly solve the problem a) Deleting the old forest by typing the following at a command prompt:

 rails d scaffold Student 

b) Create it again, but this time using the keys you want:

 rails g scaffold Student name:string 
+3
source

To begin with, the code generated by the rails generate command will be the starting point for parts of your application, which will help you jump quickly.

However, the luminaires are intended for use in unit tests. They give you a way to create a set of objects that already exist in the system, so you do not need to create them manually at the beginning of the test.

In this case, one and two are just placeholders. In a real application, you replace them with names that would be more meaningful in your tests.

If you want to add data columns to your application, fixtures are probably not suitable. They really are intended for use in tests, nothing more.

0
source

Try the following:

As for your last question, you can quickly solve the problem: a) Delete the old scaffold by typing the following at a command prompt:

 rails d scaffold Student b) Creating it again but this time with the keys you want: rails g scaffold Student name:string 
0
source

Source: https://habr.com/ru/post/1497712/


All Articles