I was wondering if anyone knew how to update the files (adding/removing/updating an attribute) produced by using the scaffold generator in ruby on rails.
For example:
scaffold student name:string lastname:string
so this will create a the associate files (controller,view,etc) with name and lastname as string attributes. When you db:migrate the project, it'll create the table in the database. However, say I want to update whether it be update it with an addition attribue (ex. studenId:integer) or if its removing or updating an attribute, how do you do that?
I tired just updating the generated files, but when I do that db:migrate it still sets the schema that is generated to what is in the table. Is there a built in script in rails that will update the table?
Any advise appreciated?
Thanks,
D
Full command in this example:
$ rails generate migration add_studentid_to_student
You need new migration file for new attributes, from console:
$ script/gnerate migration add_sudentid_to_sudent
it will generate your_app/db/migrate/8293898391_add_sudentid_to_sudent.rb, spicify in this file your new attributes:
def self.up
add_column :sudents, :studentId, :integer
end
def self.down
remove_column :students, :studentsId
end
after that, back to console:
$ rake db:migrate
and than you can edit your views, model, controller files and use new attribute
Hi Try ruby script/destroy scaffold student and then ruby script/generate scaffold student
also try reading up on rails migrations, for dropping/updating table columns.
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
Related
self explanatory from the title. I have to generate an ERD diagram reflecting customization for primary keys and timestamps in my app, but nothing i'm typing into terminal seems to work. currently using this resource: http://voormedia.github.io/rails-erd/customise.html
i tried something along the lines of
rails g model Comment attribute=primary_keys,timestamps
but my Comment model remains unchanged when i open the erd pdf. any ideas? any constructive input is appreciated. thank you
You should be able to execute the following within your Rails application root directory to generate the ERD diagram for your models:
$ bundle exec erd --attributes=content,primary_keys,foreign_keys,timestamps
Then you'll find a document called erd.pdf within your Rails application root directory.
To add columns to an existing model from the terminal; run a migration:
rails g migration AddPrimaryKeyToModel,
This will generate a new file in app/db/migrate that you would edit to look as follows:
class AddPrimaryKeyToProducts < ActiveRecord::Migration
def change
add_column :model, :id, :primary_key
add_column :model, :timestamp, :datetime
end
end
From the terminal, then run
rake db:migrate
Be aware that ActiveRecord automagically includes :id of type integer and designated as the primary_key, as well as :created_at and :updated_at of type datetime. ERD does NOT show these attributes in the diagram by default and you need to set the ERD attributes to show them in your output file.
Thank you for all your help. I found I could add the attribute using
rake erd attributes=primary_keys
and
rake erd attributes=timestamps
content is the other whole attributes.
to include primary_key and foreign_key plus attributes, do as follow :
rake erd attributes=primary_key,foreign_key,content
tested working on Rails 3.2
You can also create a config file on your project root directory like this:
.erdconfig
title: Project Model Diagram
orientation: vertical
attributes:
- content
- primary_keys
- foreign_keys
- timestamps
I'm very new to ruby on rails, been trying to play around with it in the past few days.
Basically trying to: Create a empty table, with fixed columns - Ruby on Rails
I've created a model like so:
rails g model table
rails g migration table
my tables.rb files looks like this:
class Tables < ActiveRecord::Migration
def change
add_column :table, :firstname, :string
add_column :table, :lastname, :string
end
end
(hopefully I created the columns okay)
I then run:
rake db:migrate RAILS_ENV=development
but seem to get an error no such table: table (but I thought I created it ? )
Also what is a good view I can use to see my table on localhost:3000, in a html.erb file?
What you are displaying as your tables.rb file is a migration file, not a model. Models are stored in app/models. Migrations are in db/migrate and have a name that is a datetime stamp followed by the migration name.
Your migration is performing add_column. You can't add_column until you create_table. That migration should have been built using "rails g model table". Please show all migrations with the entire filename.
Check the document that dax provided. The rails generate command uses a stylized command line. Many standard migration functions, such as creating tables and adding columns, can be automatically generated by using the correct migration name. For example:
rails g migration add_url_to_feed url:string
This will create a migration that adds a string column called url to the feed table.
Generally, migrations should do what you need. However there is another command, rake, that you will need. The reference is here. For example:
rake db:create # Create the database from config/database.yml for the current Rails.env
can create the database for you.
All,
I need clarification on how model changes need to tracked in ruby on rails. I started off by creating a model with say two fields, name and email. Here is what i have done
Created a model by running
"rails generate model user first_name:string last_name:string"
This created a model file
I then added some validations to the files created in user
Used the annotation gem to annotate the class
used "bundle exec rake db:migrate" to move my model to database which created the tables
I now want to add a couple more fields to the model. What steps do i need to follow?
Do i add columns to the database and run some command so that the model(class) is in sync with the db?
Do i delete and recreate the whole model with the new fields?
what is the recommended approach
Venu
You want to use a migration to update the existing table, you can do the entire process from the command line
Assuming you've done
rails generate model user first_name:string last_name:string
previously you would add fields like so;
rails generate migration AddFieldsToModel new_field:string another_field:string....
Rails does magic on the 'AddFieldsToModel' and works out the table name from the value for you.
Once you've created the migration you can look at it in db/migrations and then if you're happy with it just run
rake db:migrate
this will update your database to add the new fields to it. You don't need to do anything to the actual model.rb file - but you will need to re run the annotate task to have it reannotated to the model.rb file.
I am not sure what version of rails your are using .. but int rails 3.x it can be done as
rails generate migration add_fields_user
this creates a file in db/migrate/[timestamp]/add_fields_user.rb
now you can write in the file and run rake db:migrate
add_column :users , :city, :string
What you want to do is run a migration by typing. rails generate migration description_of_migration. This will create an empty migration which you can define what you want to add to your model. In your case it may be something like this:
class DescriptionOfMigration < ActiveRecord::Migration
self.up
add_column :users, :email, :string
end
self.down
remove_column :users, :email
end
end
This makes it so you can migrate both ways on the model.
rails g model Rating user_id:integer message:string value:integer
How can I completely remove this model? Thanks
When you generate a model, it creates a database migration. If you run 'destroy' on that model, it will delete the migration file, but not the database table. So before run
bundle exec rails db:rollback
rails destroy model <model_name>
For rails versions before 5.0 and higher use rake instead of rails
bundle exec rake db:rollback
rails destroy model <model_name>
Try this
rails destroy model Rating
It will remove model, migration, tests and fixtures
For future questioners: If you can't drop the tables from the console, try to create a migration that drops the tables for you. You should create a migration and then in the file note tables you want dropped like this:
class DropTables < ActiveRecord::Migration
def up
drop_table :table_you_dont_want
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
To remove migration (if you already migrated the migration)
rake db:migrate:down VERSION="20130417185845" #Your migration version
To remove Model
rails d model name #name => Your model name
Here's a different implementation of Jenny Lang's answer that works for Rails 5.
First create the migration file:
bundle exec be rails g migration DropEpisodes
Then populate the migration file as follows:
class DropEpisodes < ActiveRecord::Migration[5.1]
def change
drop_table :episodes
end
end
Running rails db:migrate will drop the table. If you run rails db:rollback, Rails will throw a ActiveRecord::IrreversibleMigration error.
How do you modify a model you've generated using modeling? For example, the model myModel originally had columns a, b and c, but I now want to add column d.
Rails 3 and above use the following code :
rails generate migration add_fieldname_id_to_tablename fieldname:string
Rails 2
ruby script/generate migration add_fieldname_to_tablename fieldname:string
This no longer works and returns the following error in Rails 3:
ruby: No such file or directory -- script/generate (LoadError)
ruby script/generate migration add_fieldname_to_tablename fieldname:string
this is the shortcut method to do exactly what you want. if you need more control, or if you have a lot of columns to add, Andrew H's answer will work fine too.
The best answer I've found so far is run this from your project root:
ruby script/generate migration add_d_column_to_myModel
Then edit the new migration file located in db/migration to look something like:
def self.up
add_column :myModel, :d, :string
end
def self.down
remove_column :myModel, :d
end
The last step will be to update your views accordingly.
Answer found here
Table functions found here