Nested attributes in rake task - ruby-on-rails

I'm trying to create a rake task to populate my DB. I need to to create 1 user with a specifc set of deatails and 99 other randomly genraterated . The details that are required are for two Models at the same time, similar to a form with nested attribues. My model uses an after_create call back to create and link a users to a team. The team name by default is the users name
the current code that I have used it producing errors
sample_data.rake
namespace :db do
desc "Fill database with sample data"
task populate: :environment do
user = User.create!(:profile_attributes => { name: Forgery::Name.full_name },
email: "test#example.com",
password: "123qwe",
password_confirmation: "123qwe")
99.times do |n|
:profile_attributes => { name: Forgery::Name.full_name },
email = Forgery::Internet.email_address
password = "password"
user = User.create!(email: email,
password: password,
password_confirmation: password)
end
end
end
Model that relys on the nested attibute.
class User < ActiveRecord::Base
after_create :set_user_on_team
private
def set_user_on_team
self.create_owned_team(:name => self.profile.name ).users << self
end
end
error message
rake aborted!
/lib/tasks/sample_date.rake:9: syntax error, unexpected tASSOC, expecting keyword_end
:profile_attributes => { name: Forgery::Name.full_name },
^
/lib/tasks/sample_date.rake:9: syntax error, unexpected ',', expecting keyword_end

I noticed some syntax errors in the 99.times block. You could try it like this:
99.times do |n|
profile_attributes = { name: Forgery::Name.full_name }
email = Forgery::Internet.email_address
password = "password"
user = User.create!(profile_attributes: profile_attributes, #my assumption
email: email,
password: password,
password_confirmation: password)
end

Related

How can allow_nil let an empty string pass the validation

I am reading Michael Hartl's Ruby On Rails Tutorial (3rd). In chapter 9, there's an example showing us how to update the user info. I got confused by the allow_nil attached here. I simplified the code as below:
class User < ActiveRecord::Base
has_secure_password
validates :password, presence: true, length: { minimum: 6 }, allow_nil: true
end
myuser=User.new(name: "Foo_bar",email: "test#example.com", password: "123456", password_confirmation: "123456")
myuser.save
myuser=User.find(myuser.id)
myuser.update_attributes(name: "Bar_Foo",email: "test#example.com", password: "", password_confirmation: "") # It will succeed! But WHY?
I understand the allow_nil: true skips the validation when the value being validated is nil. Nevertheless obviously, password: "" is not a nil value. How can allow_nil: true allows an empty string?
This behaviour is due to has_secure_password magic. In short, it check whether given password is blank or not and does not assign anything to #password if it is. Hence:
user = User.new
user.password = '' # This is not an assignment, but invocation of `password=` method.
user.password #=> nil
You can see the source code behind password= method here: https://github.com/rails/rails/blob/869a90512f36b04914d73cbf58317d953caea7c5/activemodel/lib/active_model/secure_password.rb#L122
Also note, that has_secure_password already defines default password validation, so if you want to create your own, you need to call it with has_secure_password validation: false
You can try this in irb:
user = User.new
user.password = '123'
user.password_confirmation = '123'
user.password_digest.nil? => false
user = User.new
user.password = ''
user.password_confirmation = ''
user.password_digest.nil? => true

Don't stop rake task if raise an error

I have simple rake file.
It imports data from the .xls file to the database.
But some of the data is invalid. Then data is invalid rake stop excecuting script.
But I wan to just skip this row and try with the next.
Part of my code:
data.each do |row|
username = row[0].slice!(0..2) + row[1].slice!(0..2) + rand(100).to_s
username.downcase
password = "pass1234"
User.create!(
email: row[2].downcase,
username: username,
first_name: row[0],
last_name: row[1],
expiration_date: row[3],
password: password,
password_confirmation: password)
p 'done'
end
The error is about the validation, eg.:
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Expiration date must be after 2015-10-27
Script is stopped and valid records are not added.
Wrap it in a begin/rescue block.
data.each do |row|
begin
username = row[0].slice!(0..2) + row[1].slice!(0..2) + rand(100).to_s
username.downcase
password = "pass1234"
User.create!(
email: row[2].downcase,
username: username,
first_name: row[0],
last_name: row[1],
expiration_date: row[3],
password: password,
password_confirmation: password)
p 'done'
rescue => e
# log exception or ignore it.
end
end
Note that this works because the default rescue is for StandardError. Since ActiveRecord::RecordInvalid < StandardError. It will be 'catched'.
You could rescue the specific error first if you only want to do something with those specific errors.
(For reference also see the API: http://ruby-doc.org/core-2.2.0/Exception.html)

Problems with 'seeding' data to different users

I would like to seed some data to my wiki model user the 'faker gem'. I have created three users and would like to 'spread' 20 wikis over them.
I installed the faker gem, run bundle and set up my seedfile like this:
require 'faker'
# Create an admin user
admin = User.new(
email: 'admin2#example.com',
password: 'helloworld',
role: 'administrator'
)
admin.save!
# Create a moderator
moderator = User.new(
email: 'moderator2#example.com',
password: 'helloworld',
role: 'moderator'
)
moderator.save!
# Create a member
member = User.new(
email: 'member2#example.com',
password: 'helloworld'
)
member.save!
users = User.all
15.times do
Wiki.create!(
title: Faker::Lorem.sentence,
body: Faker::Lorem.paragraph
user: users.sample
)
end
wikis = Wiki.all
puts "Seeds finished"
If I run this I get an error:
SyntaxError: /Users/marcvanderpeet/Projects/bloc/blocipedia/db/seeds.rb:37: syntax error, unexpected tIDENTIFIER, expecting ')'
user: users.sample
I dont understand why I get this error as when Im running rails c I can just type in a user:
2.1.5 :001 > u = Wiki.new
=> #
Any clues on how to fix this?
You have a syntax error: the line
body: Faker::Lorem.paragraph
should end with a comma.
Already got it, forgot a ','. Devil is in the details!

Bug when attempting to rake db:reset

I am attempting to setup an admin account for my first rails app.
This is the code I used to create the admin account:
admin = User.new(
name: 'Admin User',
email: 'admin#example.com',
password: 'helloworld',
password_confirmation: 'helloworld')
admin.skip_confirmation!
admin.save
admin.update_attribute(:role, 'admin')
Here is the code in question that is failing in Sublime:
50.times do
Post.create!(
user: users.sample,
topic: topics.sample.
title: Faker::Lorem.sentence
body: Faker::Lorem.paragraph
)
end
In terminal I am receiving this error message:
rake aborted!
SyntaxError: /Users/Alex/Desktop/code/Bloccit/db/seeds.rb:39: syntax error, unexpected ':', expecting ')'
title: Faker::Lorem.sentence
^
/Users/Alex/Desktop/code/Bloccit/db/seeds.rb:40: syntax error, unexpected ':', expecting keyword_end
body: Faker::Lorem.paragraph
^
/Users/Alex/Desktop/code/Bloccit/db/seeds.rb:41: syntax error, unexpected ')', expecting keyword_end
When I added the admin account, it appeared to add fine but after continuing on with my assignment I needed to log in with the admin account. After attempting it, it states the login information was incorrect. So I wanted to reset the DB to start over and this is where I am at now. Please help.
You missed comma separators after topic: topics.sample and title: Faker::Lorem.sentence.
50.times do
Post.create!(
user: users.sample,
topic: topics.sample, # <~~ Here
title: Faker::Lorem.sentence, # <~~ Here
body: Faker::Lorem.paragraph
)
end

Create a devise user from Ruby console

Any idea on how to create and save a new User object with devise from the ruby console?
When I tried to save it, I'm getting always false. I guess I'm missing something but I'm unable to find any related info.
You can add false to the save method to skip the validations if you want.
User.new({:email => "guy#gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" }).save(false)
Otherwise I'd do this
User.create!({:email => "guy#gmail.com", :roles => ["admin"], :password => "111111", :password_confirmation => "111111" })
If you have confirmable module enabled for devise, make sure you are setting the confirmed_at value to something like Time.now while creating.
You should be able to do this using
u = User.new(:email => "user#name.com", :password => 'password', :password_confirmation => 'password')
u.save
if this returns false, you can call
u.errors
to see what's gone wrong.
When on your model has :confirmable option this mean the object user should be confirm first. You can do two ways to save user.
a. first is skip confirmation:
newuser = User.new({email: 'superadmin1#testing.com', password: 'password', password_confirmation: 'password'})
newuser.skip_confirmation!
newuser.save
b. or use confirm! :
newuser = User.new({email: 'superadmin2#testing.com', password: 'password', password_confirmation: 'password'})
newuser.confirm!
newuser.save
If you want to avoid sending confirmation emails, the best choice is:
u = User.new({
email: 'demo#greenant.com.br',
password: '12feijaocomarroz',
password_confirmation: '12feijaocomarroz'
})
u.confirm
u.save
So if you're using a fake email or have no internet connection, that'll avoid errors.
None of the above answers worked for me.
This is what I did:
User.create(email: "a#a.com", password: "asdasd", password_confirmation: "asdasd")
Keep in mind that the password must be bigger than 6 characters.

Resources