![Rails Rails](https://cdn.nopio.com/wp-content/uploads/2017/04/28094827/n_plus_3_solution.png)
There are some people who give advice to not use rails generators and create models, controllers and etc. things manually. I don’t agree with them and my advice here is to figure out deeply how they work and then make conclusion.
May 31, 2013 Recently we had a situation where we inherited a schema and two of the models were joined using multiple foreign keys. The Rails associations API doesn’t appear to offer any good solutions to this problem. You can specify a single foreignkey and a single primarykey, but nothing really for multiple keys. We want it to default to false for new users, but existing users are considered to have already opted in, so we use the User model to set the flag to true for existing users. Rails 3.1 makes migrations smarter by providing a new change method. This method is preferred for writing constructive migrations (adding columns or tables).
In this post I will describe the most often and useful generator - it’s a model generator. I bet if you don’t use rails generators yet this post will make you to change your work. Using Rails generators saves your time, increases performance, helps to get consistent data for your application.
Bin/rails generate migration AddPostToComments post:references That will create a migration with a call to the addreference method instead of addcolumn. Addreference takes a symbol with a table name, and a symbol with the name of a model to add a foreign key for. Sep 30, 2013 Notice the id: false options you pass into the table — this asks Rails not to create a primary key column on your behalf. Changes to Model. In the model, it is essential that you add the following line in order for Rails to programmatically find the column you intend to use as your primary key.
Basic usage
Rails Generate Secret
Let’s start with simple example:
This command will generate user model with email field type of string, migration which creates users table, test for model and factory (if you have it). You are able to generate model with few fields like this:
This example will generate yet model with 3 string fields: first_name, last_name and email.
If you want to have model with different type of string pass type after field name following by : and type. Example:
![References References](https://epdf.pub/img/300x300/professional-aspnet-web-services_5a50545cb7d7bce7183ea5bb.jpg)
The whole list of available types:
You are able to pass –option parameter to generator. It will inherit generating class from passed name to achieve STI (sing table inheritance):
This example generates model:
Interesting fact that if you generate model in some scope passing model like admin/user or Admin::User:
you will get generated model in scope app/models/admin/user.rb, defined scope app/models/admin.rb which is requred to define module. Let’s see to the content of generated module:
It means that generated table name for Admin::User starts with prefix admin_users. This feature allows to have separated namespaced models as in rails code as in db schema. Very convenient and useful feature for multimodule applications for my opinion.
Advanced usage
Sometimes you have to automatically add index for columns in your migration. It’s not a problem:
Or uniq index:
Where key will be generated in struts. Set limit for field of integer, string, text and binary fields:
Special syntax to generate decimal field with scale and precision:
Pay attention that you have to wrap parameter
price:decimal{10,2}
to quotes. It’s vital and you may have incorrect behavior of generator if you don’t do it. Full explanation of this case is here.You can combine any single curly brace option with the index options:
And the last useful feature of generators - it’s options to generate reference columns (fields which are used in rails as foreign keys):
This command will generate photos table with integer field album_id and also it will add index for this field automatically. Make sure in it by looking at generated migration:
For polymorphic reference use this syntax:
Polymorphic reference with indexes:
Conclusion
As you see there a lot of useful things in rails model generator which can decrease your developing time. Thank you for reading this trolling post but anyway I hope you find it useful because I didn’t find any similar post or literature which describes rails model generator fully.
PS. Foundation for this post was got from this rails description usage which is located only in sources of rails on github.