Flowdock

Notes posted by newdark

RSS feed
January 16, 2017
0 thanks

Sort Integers

ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin16]

Sort From Greatest to Smallest

>> [1, 2, 3, 4].sort { |a, z| z <=> a }
=> [4, 3, 2, 1]
June 1, 2014 - (v4.0.2)
1 thank

Alternitive to to add flash to respond with

This is a nice way to add flash if you don’t have a any logic that needs to go around your flash.

def destroy
  @current_user_session.destroy
  respond_with @current_user_session do |format|
    format.html {redirect_to login_path, notice: "You have been logged out"}
  end
end
October 21, 2013 - (v2.3.8)
1 thank

Found out you can't set the type to number

I am sure others have tried to do this but I thought I would add a not that tells ever one. You can’t use the type field in in rails 2.3. It is always set back to type => “text”

So if you are trying to set it to type => “number” it will not work. It always goes back to text. Sorry only way around this is to use a text_field_tag and set the type in that.

<%= text_field_tag :thing, type: "number" %>

Code above is the only way to set the type. in rails 3 you can just call number_field and it will work

October 2, 2012 - (<= v2.3.8)
0 thanks

Clarification with use of update_all

I would like to point out that if you are on rails 2.3.11 or lower you will not be able to run ledermann code.

Ledermann Code

user.messages.update_all(:read => true)

If you are running 2.3 or later it you will have to use James code

James Code

Message.update_all({:read => true}, {:id => user.messages})

thanks guys for all the code help

January 4, 2012 - (v1_9_2_180)
0 thanks

Usage

I use this in views when I need to join a array of objects from a sql request here is a basic version of what I mean.

Code example

<%= @blogs.map{ |blog| blog.comment }.join(“ | ”) %>

December 31, 2011 - (v3.1.0)
0 thanks

Recommendations

I would just use the path_to_image. I find that this is what works best. As it says above it can create problems.

Here is my code

Code example

def background_path(background)
  if background
    path_to_image background.file_name.normal
  else
    path_to_image "background_preview.jpg"
  end
end

def flavor_path(flavor)
  if flavor
    path_to_image flavor.file_name.normal
  else
    path_to_image("flavor_preview.jpg")
  end
end

basic but gets the job done and it does not have problem with my pre built paths which are called image_path