When I try to generate a model using the next sentence:
rails g model SupplyRequestProfile name:string{50} active:boolean
It fails, and shows the next single line
No value provided for option '--orm'
But, when I use, skipping the fields and types:
rails g model SupplyRequestProfile
It does the job and all is normal.
I have looked if I use a reserved word of rails for my propertys in http://reservedwords.herokuapp.com/ but there is no one that im using.
The problem is not the config/application.rb, all is ok there.
So, I dont know what could be happen here, but it is like some of my property names or types are wrong or causing some strange behavior on rails generation.
What could be wrong?
Simply putting the fields names with their types inside double quotes will work:
rails generate model SupplyRequestProfile "name:string{50}" "active:boolean"
Related
I have run a migration and am getting a 500 error saying seen_by_hw does not exist. Could this be due to the fact I have used an uppercase characters when creating the ruby model. eg
seen_by_HW. Or should it be seen_by_hw
thanks
If you created the model file/class yourself then yes, this is probably the issue.
The filename should be: seen_by_hw.rb and in that the class name will be SeenByHw.
If you use the generators that comes with Rails then it would have done this for you, type rails generate in your app directory to see more info on them.
I tried to generate a rails model like so:
rails generate model signal
Which produced this error:
The name 'Signal' is either already used in your application or reserved by Ruby on Rails. Please choose an alternative and run this generator again.
I quickly discovered that "Signal" was a reserved name for models:
Reserved names with ActiveRecord models
Is there any way around this so I can have a model named "Signal", like wrapping the model in a custom Module?
I was wondering if anybody knew of a way to do something like the following:
rails generate model Foo name:string, validates: {:name, uniqueness: true}
That is, whilst declaring a model generator with some attributes, work some rails magic to add your validations at the same time.
There isn't. The rails generate model command is directly tied to database functionality. For example, you should be able to do a command like this rails generate model Foo name:string:uniq. This would force the database to require a unique string for the name. This wouldn't add anything to your foo.rb file.
Here is some more information about rails model generations:
http://travisjeffery.com/b/2012/03/generate-rails-migrations-that-automagically-add-your-change/
As others have said, there isn't currently a way to do this. Most of the special options for the rails generate model command are parsed by the parse_type_and_options method in generated_attribute.rb. The model_generator.rb will then build the model and migration files using this info.
The template for the model file that is created is model.rb. In Rails 4 this template can add in special code for belongs_to, polymorphic, and has_secure_password but not code related to validations.
The template for the migration file that is created is create_table_migration.rb. In Rails 4 this can add in special options for limit, decimal, and precision.
Since Rails 3.2 it has been possible to pass :uniq to the column definition on the rails command line.
The way to do this is relatively simple; just add :uniq after the column and type definitions, e.g.:
rails generate model Foo name:string:uniq
I have a model called MyArticle. When I try to use the command
rails generate migration AddtestToMyArticle test:string
the migration file contains only empty up/down methods. Having done this previously on a single word model name, it worked just fine and the migration up/down methods had the appropriate code.
I tried "AddtestToMy_Article" but that didn't work either. What do I need to do to work with my compound model name and the generate migration command?
You need to use
rails generate migration AddColumnToMyArticle test:string
When using 'AddColumn' you will have the appropriate code in your migration.
I just looked back over this, and while my answer is correct, it's better to have a more descriptive migration name. The user below who noticed the capitalization is right in that if you don't have each new word capitalized, Rails won't pick up on what exactly you're trying to do. So, in your question you have AddtestTo... but it should be AddTestTo....
It seems to work if you use underscores rather than CamelCase
rails g migration add_test_to_my_article test:string
Hope this helps
rails generate migration AddNewFieldToMyArticle new_field:string
I've started learning Ruby last week, and I've started learning Rails today, so bear with me.
I see that Rails come with generators that allow you to generate models/controllers or even model+controller+views bundle as 'scaffold'. This is what I am interested in.
However, I have a question. How do I set database options of the column?
eg. To generate a users table I would do:
rails g scaffold users uuid:binary name:string email:string password:binary registered_on:date number:integer default:string
Now what if I'm integrity freak and am not happy by having validation just in model/controller, but want to do database level limitations as well.
What if I want email to be 50 characters max, and number to Auto-Increment and neither of all fields is allowed to be NULL and default field must have a default of 'foo'. Is there any way to pass these requirements to generator command?
I know its possible to set these options in .rb file that is used in rake db:migrate, however it would be easier to just pass values in with 1 command.
At least some things are available, like string length, but not sure about the others.
rails g scaffold users email:string{50}
Use type modifiers between curly braces, example:
rails generate migration AddDetailsToProducts price:decimal{5,2}
More info: migrations
Use scaffold and obtain generic migration files. And, as you mentioned, do the customizations in there. This files are found in db/migrate.
After you are done customizing the fields, don't forget to do a rake db:migrate.