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?
Related
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"
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 used two different databases for my Rails application: MongoDB and MsSQL using Mongoid and activerecord-sqlserver-adapter adapter respectively. Everything is well but there is a problem while generate Model.
The problem is "how can I generate the model that relates to MongoDB or MsSQL differently?"
For example: I want to generate People model relates to MongoID and Animal model with MsSQL. While I generate with command: rails g model Animal name:string it generates the model related to mongoid. How can I generate the model Animal with ActiveRecord that means related to MsSQL.
Please help me.
Thanks
Based on Using Active Record generators after Mongoid installation? I believe this should work:
rails g active_record:model Animal name:string
First let me just check that I've understood your question correctly:
You have 2 databases and a series of models/migrations, and you want a way to tell rails which database to use when running a migration and accessing the database using your model?
If I'm in the right area then you need to add a method to your migration which overrides the default connection() method in ActiveRecord::Migration.
def connection
ActiveRecord::Base.establish_connection(:conn_name).connection
end
Where :conn_name is the name you gave your connection settings in config/database.yml
within your models add the line
establish_connection :conn_name
to the top of your model file and the model will now know which DB to connect to.
So the quick and dirty way that I have handled this in the past (due to my dev team keeping mongoid in the gem file for legacy reasons) is to comment out the out mongoid when you have to do migrations run a bundle, generate and run you migration then uncomment and run bundle again. This is far from best practices but it should work.
rails generate migration
rails generate model
rails generate scaffold
rails generate controller etc.
How these differ?
According to rails guides:
Using generators will save you a large amount of time by writing boilerplate code, code that is necessary for the app to work, but not necessary for you to spend time writing. That’s what we have computers for.
rails generate commands family used to provide simple and easy way for developer to create different object types.
rails generate migration - creates DB migration script in db/migrations directory so developer can setup his DB.
rails generate model - creates model class with associated migration, test and fixtures (test data).
rails generate scaffold - creates all nedded classes with basic logic and presentaion. It creates controller (with simple CRUD logic), model, fixtures, functional and unit tests.
rails generate controller - creates controller with associated functional tests, helper and basic views templates.
You can read more here: http://guides.rubyonrails.org/command_line.html#rails-generate
They differ in the sense that they generate different stuff.
migration will generate a database migration file,
model will generate a model(with a migration and a spec by default)
scaffold will generate a scaffold of a resource
and controller will generate a controller.
generate means it will create the files for you with boiler plate code already in place(you will still need to edit them though..but scaffold can get you working with a basic application already)
Read more about it here: http://guides.rubyonrails.org/command_line.html#rails-generate
rails generate is a command line script for quickly generating the code for various Rails' constructs.
In the example you give they differ by what they produce, with the first argument being the type of code generated. For example if I wanted to create a User model I would run:
`rails generate model user`
The model file, test file and migration would be created for me.
You should read the Rails' documentation to find more.
**rails generate model user:
The above command create a Template Object that is a mirror image of the database table.
For example, if you have a database table that is named users that has a name:string, and email:string field,then "rails generate model user" create an Object that mirrors that user table with a few addition.
Here are the similarity they both have name:string,email:string the both have the word user.
The difference are few but significant: user is Capitalized in the model name like "User".
The Table add create_by and updated_by automatically.
migration:db create the database mirror using the model as a model.RECURSION ANYONE?
I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:
script/generate scaffold model_name --skip-migration
Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?
Have you generated a schema file from your existing database? If you run the command
rake db:schema:dump
and then re-generate your scaffold this should fix the problem.
Additionally you may wish to check out Dr Nic's Magic Model generator. This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.
UPDATE
I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.
You have two choices here either
Pass in the column name pairs as well as skip-migration or
Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration