CraneWing CraneWing - 4 months ago 5x
Ruby Question

Rake db:seed refuses to insert data into a particular database column

I’m having a very peculiar thing happen when I run a seed program to add users to my Rails app -- one of the columns absolutely refuses to be populated. It’s a user populate method, with some of the elements generated by the Faker gem.

The Faker items work just fine and insert into the database, except for the Faker::Avatar method. So I decided to try my own custom string with a randomly generated number for the avatar number. But nothing worked.

Here is the Users table from the schema:

create_table "users", force: :cascade do |t|
t.string "email", limit: 191, default: "", null: false
t.string "encrypted_password", limit: 191, default: "", null: false
t.string "reset_password_token", limit: 191
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", limit: 4, default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip", limit: 191
t.string "last_sign_in_ip", limit: 191
t.datetime "created_at"
t.datetime "updated_at"
t.string "first_name", limit: 45
t.string "last_name", limit: 45
t.string "username", limit: 30
t.string "location", limit: 191
t.text "profile", limit: 65535
t.string "avatar", limit: 191

And here is the seeding method for the User:

1.times do
user = User.create!(
first_name: Faker::Name.first_name,
last_name: Faker::Name.last_name,
username: Faker::Internet.user_name,
email: Faker::Internet.safe_email,
password: Faker::Internet.password(8, 16),
location: + ', ' + Faker::Address.state,
profile: Faker::Lorem.sentence(rand(15..60)),
avatar: 'avatar' + Random.rand(1...180).to_s + '.jpg'

I am trying to figure out why I can't insert anything into the avatar column, either from Faker or my own string. It just seems strange!


If you look at the documentation for Ruby class Random here, the rand method does not support the signature you use. This should work if you change your code to:

avatar: 'avatar' + Random.rand(180).to_s + '.jpg'