I have a base model MyAppRecordBase which inherits from ActiveRecord::Base and which all other models inherit from.
I used (in config/environments/development.rb) config.generators.active_record[:parent] = 'MyAppRecordBase' to make it default. How do I make rails generate model XYZ to create a migration file for new models (it do all exept creating migration file..)?
try scaffolding:
rails g scaffold MODEL_NAME
Well, after searching for solutions, I used ActiveSupport::Concern as mentioned in this answer by #HarishShetty, And it solve it.
Related
I created a table named products in postgresql
create table products ( id int,... )
and restarted postgresql server
How to generate its corresponding Model (Product.rb) in Rails? Is it possible ?
PS : I am a beginner to Ruby on Rails
Don't create tables manually instead use ActiveRecord::Migration
For example:
rails generate model product name:string description:string price:integer
Rails automatically creates id, created_at and updated_at fields for you so you don't need to specify them while creating your model.
Then run rake db:migrate and your Products table will be created for you.
Models in rails are nothing but a subclass of ActiveRecord::Base, and by convention goes under app/models directory.
They are generally created using rails generate model command.
But you can also create them manually and place it under app/models directory.
Use the following in product.rb
class Product < ActiveRecord:: Base
end
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 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'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.
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