How can you do a conditional where clause using Active Record

It is possible to chain where statements residentials = Residential.where(:is_active => true) residentials = residentials.where(:other_thing => true) if param_was_passed This should work. Make sure this is not the last line in a function call; repeat the residentials variable as the last line in that case. (As per @digger69 comment)

Where to override current_user helper method of devise gem

According to the module Devise::Controllers::Helpers, current_user (together with all other devise helpers) is added to ApplicationController, which means that you can override it in this way: # in application_controller.rb def devise_current_user @devise_current_user ||= warden.authenticate(scope: :user) end def current_user if params[:user_id].blank? devise_current_user else User.find(params[:user_id]) end end

Saving the images Dimensions (width and height) in Paperclip?

Just for the sake of completeness, even though previous answers already show good enough suggestions. You can utilize Paperclip event handlers instead of Rails callbacks. In this case, size will be recalculated only when image changes. (If you’re using S3 for storage, this can save quite some time) has_attached_file :image, :styles => … after_post_process :save_image_dimensions … Read more

How to write columns header to a csv file with Ruby?

I would recommend to use the CSV-library instead: require ‘csv’ CSV.open(‘test.csv’,’w’, :write_headers=> true, :headers => [“numerator”,”denominator”,”calculation”] #< column header ) do|hdr| 1.upto(12){|numerator| 1.upto(12){ |denominator| data_out = [numerator, denominator, numerator/denominator.to_f] hdr << data_out } } end If you can’t use the w option and you really need the a+ (e.g., the data isn’t available all at … Read more