hello.
I'm trying to add a column called product_size to my order_items table in my schema.
Do I do this by:
$ rails g migration add_product_size_to_order_items
or
$ rails g migration add_product_size_to_OrderItem
or otherwise?
Thanks!
You should use
rails g migration add_product_size_to_order_items
then add also this line product_size:data_type
sample:
rails g migration add_product_size_to_order_items product_size:decimal
Related
I'm new to RoR, and need some help on migration.
I have an existing database containing a "date" column in a mysql database.
I need to store them into 3 separate integer fields (year, month, date) (don't ask me why, because my clients are idiots)... and need to migrate the existing column into the new columns (The existing database already contain data). I'm just wondering how could I do that in migration?
Thank you
After some digging... solved it.
Migration is not the preferred way of doing this, rake task should be used.
First, generate the new columns using migrations, than add the new attr_accessible in the model.
To generate a rake task, follow http://railsguides.net/how-to-generate-rake-task/ link.
And use the following code, assuming the model containing the date column is called Visit
namespace :visit_date_migration do
desc "Migrate visit date into 3 integer columns for year/month/date"
task :migrate_date => :environment do
Visit.all.each do |visit|
unless visit.visit_date.nil? || visit.visit_date == 0
year = visit.visit_date.year
month = visit.visit_date.month
day = visit.visit_date.day
visit.visit_date_year=year
visit.visit_date_month=month
visit.visit_date_date=day
visit.save!
end
end
end
end
And run the rake task like a normal task.
I want to run sql commands directly in my rails application. Is there any gem to do this?
For example:
I want to delete a user , who is having id 2 using below sql command
delete from users where id=2
ActiveRecord::Base.connection.execute("delete from users where id=2")
Yes. We can do that by using query_exec
If you want to run SQL from your browser try https://github.com/igorkasyanchuk/rails_db
In rails 3 if I write: Model.from('models') arel generates the following sql:
select "models".* from models
In rails 4 the same arel generates the following sql:
select "models".* from 'models', NULL
The table name is wrapped in quotes and ', NULL' is appended. How do I use arel to give me the same results as in rails 3?
I was leveraging the original behavior so I could run a fairly complex with recursive query against postgres. In rails4, postgres is choking when it gets to the single quote preceding value I give from.
Is there a better way to do a with recursive query? Or, is there a way to query with arel so it works as before?
This appears to be an incompatibility with the squeel gem (the master branch as of today).
EDIT I've sent a pull request to squeel's master branch. Fixes the issue for me.
EDIT 2: Merged into master
I've got a .sql file that I'd like to load into my Rails database using a Rake task. How can I do this?
The easiest way:
bundle exec rails db < $SQL_FILE
example:
bundle exec rails db < my_db.sql
The Easy Way
This works for simple cases.
ActiveRecord::Base.connection.execute(IO.read("path/to/file"))
Solution found on the Ruby On Rails mailing list from 2006 (but still works in 2011 on Rails 3.1).
Footnotes
This related question implied this solution, but rejected it for big imports. I wanted to show it explicitly, since it works for smaller ones.
The file I was trying to import contained a LOCK TABLES followed by an insert. The data was for a MySQL database. Mysql2 said it had an invalid SQL syntax error until I removed the lock and unlock statements.
On MySQL this gave me a syntax error. Splitting the sql into statements made it work.
sql = File.read(sql_file)
statements = sql.split(/;$/)
statements.pop # remove empty line
ActiveRecord::Base.transaction do
statements.each do |statement|
connection.execute(statement)
end
end
Hi i have a table in which there are two columns from and to of type datatime i want to change the datatype from datetime to date. i dont know the exact command for chagning column type in migrations like rails g
From the command line, run:
rails generate migration change_data_type_for_table_column
Write your migration as:
change_table :table do |t|
t.change :column, :type
end
Use change_column.
change_column(:table, :column, :date)