2

I have been basically java programmer and beginner for Ruby Rails. In java, to seed database ant task is run, the ant task execute SQL script. SQL script is basically set of insert statements.

I expect there must be some equivalent of ant task on Ruby Rails platform for running sql script?

Edited in response to answer by Nikita:

Although one can use Migration as one of way for seeding data. But I don't want to do rework of writing migration classes corresponding to sql scripts. so i need some solution through which i have to exec sql script file only. I want to manage by database only through sql code.

3 Answers 3

5

I personally don't like using migrations to seed large amounts of "static" data. You can easily execute sql statements from db/seeds.rb by:

# Empty the soon-to-be-seeded table
MyModel.delete_all

# Get the current DB connection
connection = ActiveRecord::Base.connection();

# Execute a sql statement
connection.execute("Insert INTO my_models (id, name) VALUES
    (1, 'Example 1'),
    (2, 'Example 2'),
    (3, 'Example 3');")

Basically, once you have the current connection, you can just call a sql statement useing connection.execute("SQL CODE HERE").

Sign up to request clarification or add additional context in comments.

Comments

2

If you generated your rails application from console, you should have db/seeds.rb file. It populates database tables with default values.

Then, you can execute rake db:seed.

The autogenerated seeds.rb contains this example, which should clear any doubts

# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#   
#   cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
#   Major.create(:name => 'Daley', :city => cities.first)

Note, that you do not put any sql statements in it (not usually). Although you can still execute sql from the seed file or migration, using ruby code and models is preferred.

on the comment
Maybe we have different understanding of what 'seed' means.
Anyway, migrations should help here.
It's too large topic to cover it in my post, so please read the guide. Although it focuses on creating/deleting tables, you can manipulate data just as well in a migration.

7 Comments

I don't need default value. I want to seed lot of data in database, for that purpose i need to run sql scripts. Besides db/seeds.rb has all of code lines commented with no statement for dumping default values.
Maddy.Shik -- Nikita is correct, you should run using seeds. If you just want to import existing data into a database, just run the scripts outside of rails.
@Maddy "Besides db/seeds.rb has all of code lines commented with no statement for dumping default values." Can you clarify?
seeds.rb contains code exactly as you specified in your answer.
@Maddy From the link above: "If you need to perform tasks specific to your database (for example create a foreign key constraint) then the execute function allows you to execute arbitrary SQL." Or you can follow Jesse's suggestion and do it outside of rails.
|
0

For initial, static daata use the seeds file as suggested.

For other purposes you can just wrap the entire insert statement inside an active record statement, e.g. find_by_sql("insert into mytable (col1, col2) values (1,2)"). using the "find by" isn't great reading but it'll do it.
You can also just use ActiveRecord::Base.connection.execute(sql) and put your sql there. Whatever you have such as insert, etc. will get executed. Usually you'll need a ; at the end if yu have multiple statements.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.