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.
Related
In ruby on rails normally use SQLite as a database.so a special property of rails called migration are work.but when we use "mongodb" as a database in rails.I see there have no migrate folder in the db directory.
Is there any way to use this migration property in rails when use mongodb.
According to the documentation here, db:migrate: Exists only for dependency purposes, but does not actually do anything.
However, because I am not sure of what version of rails you're using, how your project was setup and if you intend using just mongodb I will describe the process for both possibilities from scratch with all assumptions if any clearly stated.
This approach assumes you want to use mongodb alone
Create your Rails app with the --skip-active-record switch.
Remove sqlite3 from your Gemfile
add gem 'mongoid' to your Gemfile
and run bundle
Run rails g mongoid:config
Check your application.rb file and make sure that inside the 'class Application' you have this line Mongoid.load! './config/mongoid.yml' It's sometimes not included when the config is generated, but is needed to use Mongoid.
Mongoid is ready to go.
The Rails generators for model, scaffold, etc have been overridden by Mongoid. Any models, scaffolds etc that you create will create classes that include the Mongoid::Document module instead of inheriting from ApplicationRecord in the models folder.
For instance, when you run
rails g model person first_name last_name email_address
if you open up the file app/models/person.rb
You'd see
class Person
include Mongoid::Document
field :first_name, type: String
field :last_name, type: String
field :email_address, type: String
end
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 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.
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
When running:
rails generate migration <someaction> field:type
I can see it is performing two actions:
Call invoke active_record
Create a migration file.
I understand perfectly why it is generating the migration file, but why does it invoke ActiveRecord? This bothers me because what if I want to create the migration file manually? how would I mimic this invocation (if it's even necessary..)?
The MigrationGenerator generator:
1) Loads your ORM (which by default in Rails is Active Record) to have it extend the correct 'ORM'::Migration class (again by default this is ActiveRecord::Migration)
2) It is itself an extension of the NamedBase generator which sees if your running Active Record to decide if it should pluralize the table names. This is so if you run
rails generate migration AddPartNumberToProducts part_number:string
or
rails generate migration AddPartNumberToProduct part_number:string
You get the same results in your file.
So the short answer is, you do not need to invoke active_record to create a migration by hand, but if you do and you are using Active Record make sure that your table names are pluralized in your migration file.
activerecord gem is invoked to generate the migration file.
If you look closely, the super class of a migration file is an ActiveRecord::Migration class.