require 'active_record'
desc "Get all the pico HTML mess into a Ruby model"
task :muck => :environment do
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
puts "About to process 'uge SQL file...\n"
execute "#{RAILS_ROOT}/db/html_template.sql"
puts "File executed\n"
end
But when I try it, I get this error:
undefined method `execute' for main:Object
Anyone know how to use a raw sql file in a rake task?
--
Posted via http://www.ruby-forum.com/.
>
> How do I execute a raw SQL file from a rake task? Right now I have
> this:
>
> require 'active_record'
> desc "Get all the pico HTML mess into a Ruby model"
> task :muck => :environment do
> ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
> puts "About to process 'uge SQL file...\n"
> execute "#{RAILS_ROOT}/db/html_template.sql"
> puts "File executed\n"
> end
>
> But when I try it, I get this error:
> undefined method `execute' for main:Object
>
ActiveRecord::Base.connection.execute(...)
Fred
>
That seems to work if actual sql code is put in the parenthesis, but how
can I run SQL out of a sql file in the rake task?
Doing this in the rake task:
ActiveRecord::Base.connection.execute("#{RAILS_ROOT}/db/html_template.sql")
gives me this error:
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual
that corresponds to your MySQL server version for the right syntax to
use near '/projects/trieste2/db/html_template.sql' at line 1:
/projects/trieste2/db/html_template.sql
How can I run the sql that is in the sql file?
Yeah, I can. Now I ran into good ol' Error 1136: Column count doesn't
match value count at row 1
It really looks to me like it should work, but I'm sure there's some
tiny thing that is wrong.
insert into html_templates (id, name, htmlcode, style, js, count)
values('1000','Mega
Template','<html><body>Hello</body></html>','\r\nborder:1px;
\r\nborder-color:#333333; \r\nborder-style:solid;\r\n}\r\n','','0');
Okay, figured it out. I'll post this in case someone else stumbles
around looking for how to fix this error.