Create a devise user from Ruby console

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 … Read more

The Ruby %r{ } expression

%r{} is equivalent to the /…/ notation, but allows you to have “https://stackoverflow.com/” in your regexp without having to escape them: %r{/home/user} is equivalent to: /\/home\/user/ This is only a syntax commodity, for legibility. Edit: Note that you can use almost any non-alphabetic character pair instead of ‘{}’. These variants work just as well: %r!/home/user! … Read more

How to get current path with query string using Capybara

I’ve updated this answer to reflect modern conventions in capybara. I think this is ideal since this is the accepted answer, and what many people are being referred to when looking for a solution. With that said, the correct way to check the current path is to use the has_current_path? matcher provided by Capybara, as … Read more

test a file upload using rspec – rails

You can use fixture_file_upload method to test file uploading: Put your test file in “{Rails.root}/spec/fixtures/files” directory before :each do @file = fixture_file_upload(‘files/test_lic.xml’, ‘text/xml’) end it “can upload a license” do post :uploadLicense, :upload => @file response.should be_success end In case you were expecting the file in the form of params[‘upload’][‘datafile’] it “can upload a license” … Read more

Rails: Using build with a has_one association in rails

The build method signature is different for has_one and has_many associations. class User < ActiveRecord::Base has_one :profile has_many :messages end The build syntax for has_many association: user.messages.build The build syntax for has_one association: user.build_profile # this will work user.profile.build # this will throw error Read the has_one association documentation for more details.

How to select where ID in Array Rails ActiveRecord without exception

If it is just avoiding the exception you are worried about, the “find_all_by..” family of functions works without throwing exceptions. Comment.find_all_by_id([2, 3, 5]) will work even if some of the ids don’t exist. This works in the user.comments.find_all_by_id(potentially_nonexistent_ids) case as well. Update: Rails 4 Comment.where(id: [2, 3, 5])

Rails 2.3-style plugins and deprecation warnings running task in Heroku

Are you using Heroku? Heroku will inject plugins in Rails 3.x applications .. To avoid this injection in Rails 3, include the rails_12factor gem in your application. (Heroku Ruby Support 2013-10-26) The rails_12factor gem is also required in rails 4. If this gem is not present in your application, you will receive a warning while … Read more