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
Related
I am writing a Ruby on Rails app. I have created a model like so:
rails generate model Post user:references header:string body:text topic:string
And then I created a model User like so:
rails generate model User name:string email:string password:string
But running
db:migrate
Gives me the error:
PG::UndefinedTable: ERROR: relation "users" does not exist
So I guess I messed up with the order of the table creation and should have created the model User first and then migrate before creating the Post model?
You can rename one of the two new migrations in db/migrate/ to change the numeric timestamp in the filename, and thereby change the order they're run.
I created a Refinery::Carts engine which is in a separate directory all by itself. It has a Cart model. Now I wanted to create a LineItem model. I tried to run
rails generate model LineItem cart:references product_id:string qty:integer 'unit_price:decimal{6,2}' 'virtual:boolean{false}' address:references
However it complained
Please first run 'rake refinery:testing:dummy_app' to create a dummy Refinery CMS application.
So I ran that and bundle install, but now that I ran the rails generate model, it put the model into spec/dummy folder! I tried to use rails generate model Refinery::Carts::LineItem ... but it complained the files already existed
/usr/local/rvm/gems/ruby-2.0.0-p247#bk_development/gems/factory_girl-4.2.0/lib/factory_girl/decorator.rb:10:in `method_missing': Factory alrea dy registered: line_item (FactoryGirl::DuplicateDefinitionError)
So how do I use rails generate model to create additional models for a RefineryCMS engine/extension? Must I do it all manually?
You'll want to check out this guide from refinerycms http://refinerycms.com/guides/multiple-resources-in-an-extension
Taken from that guide: To add a Place model to an extension named Events
rails g refinery:engine place name:string --extension events --namespace events
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
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