Rails 4 - nil value on rake db:seed [duplicate] - ruby-on-rails

I have a CarrierWave rails uploader. I want to seed the database with fake users so I'm trying to add the images in with the same seed file. The images are in a common storage, so if I can just get the avatar strings in the database they'll work. When it saves the users though the image's aren't sticking.
# db/seeds.rb
user1 = User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :avatar => "v1357014344/bdylan.jpg"
user1.save
# IRB
User.first
=> #<User id: 1, email: "test1#test.com", name: "Bob Dylan", avatar: nil>
> a = User.first
> a.avatar = "v1357014344/bdylan.jpg"
> a.save
(10.7ms) commit transaction
=> true
> a
=> #<User id: 1, email: "test1#test.com", name: "Bob Dylan", avatar: nil>

You will have to insert the data in the following way.
File.open(File.join(Rails.root, 'test.jpg'))
So the entire user create would look like
User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :avatar => open("v1357014344/bdylan.jpg")
Related question

Besides using open() as Nishant suggestions, you can also specify remote_avatar_url to manually set the remote URL.
User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :remote_avatar_url => "http://upload.wikimedia.org/wikipedia/commons/2/28/Joan_Baez_Bob_Dylan_crop.jpg"
Thanks to JosephJaber for suggesting this in Seeding file uploads with CarrierWave, Rails 3
I used this approach to populate some video URLs for CarrierWave Uploader in my seeds.rb

To update the database directly and skip out on CarrierWave:
model[:attribute] = 'url.jpg'
model.save

Related

Creating user from rails console command?

I am newbie in Ruby, I am trying to install one app that says:
lobsters$ rails console
Loading development environment (Rails 4.1.8)
irb(main):001:0> User.create(:username => "test", :email => "test#example.com", :password => "test", :password_confirmation => "test", :is_admin => true, :is_moderator => true
irb(main):002:0> Tag.create(:tag => "test")
When I run rails console, it outputs "create some file" So how do I create user?
Try this :
User.create!(username: "test", email: "test#example.com", password: "test", password_confirmation: "test", is_admin: true, is_moderator: true)
In practice, Ruby programmers reserve ! to adorn the names of methods that do something unexpected, or perhaps a bit dangerous
So in this case, the "unexpected" result is that an exception is
raised instead of just failing and returning false.

Override Rails Uploader to seed database

I have a CarrierWave rails uploader. I want to seed the database with fake users so I'm trying to add the images in with the same seed file. The images are in a common storage, so if I can just get the avatar strings in the database they'll work. When it saves the users though the image's aren't sticking.
# db/seeds.rb
user1 = User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :avatar => "v1357014344/bdylan.jpg"
user1.save
# IRB
User.first
=> #<User id: 1, email: "test1#test.com", name: "Bob Dylan", avatar: nil>
> a = User.first
> a.avatar = "v1357014344/bdylan.jpg"
> a.save
(10.7ms) commit transaction
=> true
> a
=> #<User id: 1, email: "test1#test.com", name: "Bob Dylan", avatar: nil>
You will have to insert the data in the following way.
File.open(File.join(Rails.root, 'test.jpg'))
So the entire user create would look like
User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :avatar => open("v1357014344/bdylan.jpg")
Related question
Besides using open() as Nishant suggestions, you can also specify remote_avatar_url to manually set the remote URL.
User.create :email => "test1#test.com", :password => "testing", :name => "Bob Dylan", :remote_avatar_url => "http://upload.wikimedia.org/wikipedia/commons/2/28/Joan_Baez_Bob_Dylan_crop.jpg"
Thanks to JosephJaber for suggesting this in Seeding file uploads with CarrierWave, Rails 3
I used this approach to populate some video URLs for CarrierWave Uploader in my seeds.rb
To update the database directly and skip out on CarrierWave:
model[:attribute] = 'url.jpg'
model.save

gem Paperclip interpolation

Good day.
I have rails 3.1. and gem Paperclip
in my application to manage companies contracts:
MODEL
model/contract.rb
has_many :contract_files
model/contract_file.rb
has_attached_file :data
CONSOLE
Loading development environment (Rails 3.1.0)
1.9.2p290 :001 > cont = Contract.first
Contract Load (0.1ms) SELECT "contracts".* FROM "contracts" LIMIT 1
=> #Contract id: 1, organization: "Com.org", and etc ....
1.9.2p290 :002 > cont.contract_files
ContractFile Load (0.2ms) SELECT "contract_files".* FROM "contract_files"
WHERE "contract_files"."contract_id" = 1
=> #[ContractFile id: 88, caption: "asdf", and etc ...]
QUESTION
Be kind, tell me please, how can I extract Contract id: 1 in
model/contract_files.rb
has_attached_file :data,
:url => "/assets/paperclip/:contract_id/:filename"
EXAMPLE WHAT I WANT TO GET
On http//localhost:3000/contracts/1 get such files pathes:
http//localhost:3000/contracts/1/assets/paperclip/1/XXX.pdf
http//localhost:3000/contracts/1/assets/paperclip/1/XXY.pdf
http//localhost:3000/contracts/1/assets/paperclip/1/XXZ.pdf
Thanks a lot for helping.
If I get it clear you might do it this way:
Contract.find( params[:id] ).contract_files.map { |cf| cf.data.url }
# Dont't forget to handle nonexistent id
Upd
To place contract_id in url you should recover default paperclip :url and :path parameters this way:
#model/contract_file.rb
has_attached_file :data, :path => "public/contracts/:parent_id/assets/paperclip/:id.:extension", :url => "/contracts/:parent_id/assets/paperclip/:id.:extension"
Paperclip.interpolates :parent_id do |a, s|
a.instance.contract.id
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.

Storing information inside YAML

I looked through the YAML for ruby documentation and couldn't find an answer.
I have a list of several employees. Each has a name, phone, and email as such:
Employees:
Name | Phone | Email
john 111 a#b.com
joe 123 b#a.org
joan 321 c#a.net
How would I write the above information in YAML to end up with the following ruby output?
employees = [ {:name => 'john', :phone => '111', :email => 'a#b.com'}, {:name => 'joe', :phone => '123', :email => 'b#a.org'}, {:name => 'joan', :phone => '321', :email => 'c#a.net'} ]
This is how I parse the YAML file:
APP_CONFIG = YAML.load_file("#{RAILS_ROOT}/config/config.yml")
Thank you!
- name: john
phone: 111
email: a#b.com
- name: joe
phone: 123
email: b#a.org
- name: joan
phone: 321
email: c#a.net
Output is string keys, not symbol keys, but you can make that conversion yourself if really necessary.
Note that standard yaml which ships with Ruby 1.8.6 is NOT UTF / Unicode safe.
Use either Ruby 1.9 yaml or Ya2YAML gem if you'll have any non-ASCII test.
Also, the easiest way to see what the yaml input should be is to create (in Ruby irb), an example of your data structure. Then turn it into yaml text by using the .to_yaml object class method.
Eg
require 'yaml'
# create your structure
a = [{'name' => "Larry K", 'age' => 24,
'job' => {'fulltime' => true, 'name' => 'engineer'}}]
a.to_yaml
=> "--- \n- name: Larry K\n job: \n name: engineer\n fulltime: true\n age: 24\n"
# then substitute line breaks for the \n:
---
- name: Larry K
job:
name: engineer
fulltime: true
age: 24

Resources