Created
March 5, 2013 12:08
-
-
Save Mizpah/5089898 to your computer and use it in GitHub Desktop.
Trying to work out the syntax of a seeds.rb file for a rake:dbseed command. I have a reference - the first file -- however in my example I do not have an entity 'name' in my db, I have the entity 'username'. Clearly I am not identifying correctly that the user is both the reference to the column, and an object - So after several attempts I guess…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'DEFAULT USERS' | |
user = User.find_or_create_by_email :username => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, | |
:password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup | |
puts 'username: ' << user.username | |
user.add_role :admin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts 'DEFAULT USERS' | |
user = User.find_or_create_by_email :name => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, | |
:password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup | |
puts 'user: ' << user.name | |
user.add_role :admin | |
user2 = User.find_or_ |
And the answer is! note the confution of the name method on bah,vs the value of username on bah!
puts 'ROLES'
YAML.load(ENV['ROLES']).each do |role|
Role.find_or_create_by_name({ :name => role }, :without_protection => true)
puts 'role: ' << role
end
puts 'DEFAULT USERS'
foo = User.find_or_create_by_email :username => ENV['ADMIN_NAME'].dup, :email => ENV['ADMIN_EMAIL'].dup, :password => ENV['ADMIN_PASSWORD'].dup, :password_confirmation => ENV['ADMIN_PASSWORD'].dup
puts 'username: ' << foo.username
foo.add_role :admin
bah = User.find_or_create_by_email :username => 'Mizpah', :email => 'mizpah@mmofish.com', :password => 'changeme', :password_confirmation => 'changeme'
puts 'username: ' << bah.username
bah.add_role :VIP
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I would go for something like :
User.where(:email => ENV['ADMIN_EMAIL'].dup).first_or_create do |user|
user.username = ENV['ADMIN_NAME'].dup
email = ENV['ADMIN_EMAIL'].dup
password = ENV['ADMIN_PASSWORD'].dup
password_confirmation => ENV['ADMIN_PASSWORD'].dup
end