When using rails scaffolding
rails g Somemodel nation:references
the fixtures automatically generated in the test directory take this form
one:
nation: one
why is that? the datatype is an integer and the attribute is nation_id.
Related
I type:
rails g rspec:controller application
But nothing appears in the spec file?
If you type script/rails generate, the only RSpec generator you'll
actually see is rspec:install. That's because RSpec is registered with
Rails as the test framework, so whenever you generate application
components like models, controllers, etc, RSpec specs are generated
instead of Test::Unit tests.
rails g rspec:integration [name]
controller
helper
install
integration
mailer
model
observer
scaffold
view
example:
=> rails g rspec:integration controller
=> create spec/requests/controllers_spec.rb
I am new to Rails. I want to know the difference between these two methods of generating migrations:
rails g migration MigrationName
rails g model MigrationName
Is preferred for making alterations to the tables that already exists. What is the real difference between these 2?
Well the main difference is that the second one should be rails g model ModelName doesn't just create a migration, but also creates a model file and a spec file for that model. It also will most generate a create table migration, whereas with rails g migration MigrationName, you can just do very specific migrations such as adding an index, or adding/removing columns. Sections 2.1 and 2.2 will help you get a better grasp: http://guides.rubyonrails.org/migrations.html
rails g migration Filename parameters ...
This one generates a file in which you can write migration code. Like creating index or drop, ...
rails g model Tablename field field field:type ....
This on generates a file to generate a table with the give parameters.
See more when running rails g model
I'm trying to generate some models but they are being generated without attributes.
I'm using a linux system and the rails version is:
rails --version
Rails 4.0.0
I've tried to generate the models using this commands:
rails g scaffold Bsdsd description:string test:string oaso:integer
and
rails g model Asdsd description:string test:string oaso:integer
The first results in this empty class model everything else ok:
class Bsdsd < ActiveRecord::Base
end
The second results in test files, migrations file(that contains the attributes) and this class model:
class Asdsd < ActiveRecord::Base
end
How can I correct this behavior?
Model attributes are inferred from database columns, so you don't need them specified in model classes.
In Rails 3.2 you had (if I remember correctly)
# attr_accessible :description, :test, :oaso
line generated. But protected attributes are deprecated in Rails 4.0 and replaced by strong parameters mechanism.
Nothing you're doing is wrong. But you're checking the wrong files. Look for CreateAsdsdsMigration (in the migrations directory) file and you'll see the auto-generated fields there
For those coming from Grails or Django, note that Rails creates the database FIRST-- not the other way around, where domainclass.groovy or models.py creates the database tables for you AFTER you define them. Look for yourapp/db/schema.rb and inside are all your classes and their field definitions.
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 wrote a simple migration file that creates around ten tables. It all created perfectly. Now I need to create ten equivalent model files in the app/models folder. I can do it manually. But I am wondering if there is any rake task available to do this.
Tips/advise on this is much appreciated.
Automatically: http://magicmodels.rubyforge.org/magic_model_generator/
Manually: http://forums.devshed.com/showpost.php?p=1957164&postcount=2
You can create a model and migration using the same rails model generator. This will also create a unit test and fixtures.
Rails 2.3.x:
script/generate model Person name:string
Rails 3.0.x
rails g model Person name:string
You can also use the following options (taken from Rails 2.3.8 and might have changed in 3.0)
Options:
--skip-timestamps Don't add timestamps to the migration file for this model
--skip-migration Don't generate a migration file for this model
--skip-fixture Don't generation a fixture file for this model