ramya
ramya

Reputation: 928

db migrations to insert a row in ruby

I am having a text file that contains users name.I want to extract users name from the text file and insert it into the table via db migrations.Here the extraction part works fine.There are no errors displayed with rake db:migrate. But the data is not seen in the database.

class AddUsers< ActiveRecord::Migration
   def self.up
     i=0
     File.open("users").each do |line|
     if i>=4 && line=~/^(\s)+[a-z]+/
         word=line.split("|")
         word[0]=word[0].strip
         email=word[0]+"\@sandvine.com"
         puts "word=#{word[0]},email=#{email}"
         User.create :name =>#{word[0]}, :email => #{email}
         puts "created"
     end
     i=i+1
   end
end

 u=User.create(:name => "ramyameena", :email  => "[email protected]",:password=>"sandvine",:roles=>{:id=>2,:name=>"Tester"})
=> #<**User id: nil**, name: "ramyameena", created_at: nil, updated_at: nil, email: "[email protected]", encrypted_password: "$2a$10$qIfRLKZlxviag9E0Gzvp8e3VKkOCaXraP7PnJC6vGMN....", reset_password_token: nil, remember_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil>

irb(main):012:0> u.errors.inspect
=> "#<OrderedHash **{:roles=>[\"can't be blank\"]**}>"

My users model:

class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates :name, :presence => true, :uniqueness => true
validates :roles, :presence => true
has_many :user_role_assignments
has_many :roles, :through => :user_role_assignments
has_many :tester_release_assignments
has_many :releases, :through => :tester_release_assignments
has_many :releases
has_many :ic_runs
accepts_nested_attributes_for :user_role_assignments
attr_accessible :email, :name, :password, :password_confirmation, :role_ids

thanks, Ramya.

Upvotes: 0

Views: 423

Answers (2)

Thilo
Thilo

Reputation: 17735

The #{...} syntax is used to interpolate ruby in a string. You're using it correctly here:

puts "word=#{word[0]},email=#{email}"

but then incorrectly here:

User.create :name =>#{word[0]}, :email => #{email}

Here the # is actually commenting out the rest of the line (as you can even tell by the syntax highlighting here). It's curious that you don't get any syntax errors when running this code, but at best the result is undefined. What you want is this:

User.create(:name => word[0], :email => email)

as @dexter alread noted.

BTW, this is one reason to use braces even if you don't have to, because this:

User.create(:name =>#{word[0]}, :email => #{email})

would definitely throw a syntax error since the closing brace is commented out.

Upvotes: 1

dexter
dexter

Reputation: 13593

I am not sure about the create method you are calling. Everything after => would be a comment. Should have been

User.create :name => word[0], :email => email

Also, instead of a migration, it's better to add this code to db/seeds.rb as this is seed data.

You can load the table data using rake db:seed

Upvotes: 1

Related Questions