Notes posted to Ruby on Rails

RSS feed
December 3, 2013
0 thanks

usage examples

For detailed usage examples of where see “Conditions” under ActiveRecord::Base.

December 2, 2013 - (v3.2.13)
0 thanks

Redirect Status

Redirect 302

get "/accounts", :to => redirect("/", :status => 302)
November 26, 2013
0 thanks

Updated list of statuses

The list of supported statuses is now in the layout & rendering rails guide:


November 22, 2013
0 thanks
November 18, 2013
0 thanks

BEWARE of options[:value] in a partial

No problem with new records however,

the following line will not display the current @ecard value on EDIT but ‘contact@host.com’.

text_field :ecard, :sender, :value => 'contact@host.com'

Don’t let your views take control, but if you insist:

text_field :ecard, :sender, :value => f.object.ecard.blank? ? 'contact@host.com' : f.object.ecard

This doesn’t make sense, since when this object was created they deleted the default, which made it blank. Why would they want it back?

November 13, 2013 - (v3.2.13)
0 thanks


Just for completeness (it should behave like this), comparing ranges that start and end with the same value will overlap, e.g.

(1..5).overlaps?(5..10) # => true
November 13, 2013 - (v3.2.13)
0 thanks

Take care with time ranges

Trying this in the console:


will blow up…

It’s fine with Dates though:

(1.day.from_now.to_date..5.days.from_now.to_date).overlaps?(5.days.from_now.to_date..10.days.from_now.to_date) # => true
November 8, 2013
0 thanks

submit button with rails javascript's onclick event handler method

Code example

<%= f.submit ‘Create User’, class: ‘buttons’, :onclick => “validate_user_form_and_submit()” %>

November 7, 2013
1 thank


Where you see:

HelperData.new(datetime, options, html_options).select_hour

The correct would be:

HelperDate.new(datetime, options, html_options).select_hour

Class name and class instance must be same name.

Code works fine in Rails 3.2.13.

November 5, 2013
0 thanks

The "instance-level equivalent" ActiveRecord::Base#increment is NOT atomic

Typically, you want to increment counters atomically, so the class method ActiveRecord::Base.increment_counter is the right choice.

Also, there is an issue with the increment example below as it does not save automatically:

item = Item.find(1)
item.foo_count # => 0
item.foo_count # => 1
item.foo_count # => 0
item.foo_count # => 1
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 16, 2013
0 thanks

Erb tags

The <% -%> is not being used since Rails 3.

The example above should be changed to:

<%= form_tag('/posts') do %>
  <div><%= submit_tag 'Save' %></div>
<% end %>
October 11, 2013 - (<= v3.2.13)
1 thank

Bug - this is not working as documented

The stacking of several after_commit lines is not working. The last line overwrites the ones before.

Using an array for the :on options is also not working.

For details look here: http://github.com/rails/rails/issues/988#issuecomment-12653474

October 10, 2013
0 thanks

Classy guys...

:fav_bra_size - really?

October 2, 2013
0 thanks

You have explain well

You have explain well about fragment caching as I was in need of this information. http://autobodycarparts.com

October 2, 2013
0 thanks


Thanks for the coding part it was really helpful for me, I would apply it on my project. http://www.koolchart.com

October 1, 2013
0 thanks

Update multiple attributes with raw sql

You can update multiple attributes with sql for each record being updated by using the following syntax:


Model.update_all("foo = 'bar', baz = 'bat'")
September 24, 2013
0 thanks

Missing documentation for options hash

The deprecated function (http://apidock.com/rails/ActiveRecord/Serialization/to_json) has more useful documentation for the options hash.

September 13, 2013
0 thanks

Edge case

NilClass#try doesn’t check for methods on itself:

nil.blank?        #=> true
nil.try :blank?   #=> nil
September 11, 2013 - (v3.2.13)
0 thanks

touch in rails

touch is used to update the updated_at column you can create a model instance and update the updated_at value on it

Let say we have one user model

user = User.first

next you can write


so it will run the query for you

UPDATE `users` SET `updated_at` = ‘current_date_time’ WHERE `users`.`id` = 1

Alternatively, to update some other column with the current date and time use:


September 8, 2013
0 thanks

without a bang

We can use hash#slice if we want an Hash#extract (without bang) like behavior.

August 24, 2013
1 thank

expires_in option

@concept47 do you really need to check the fragment in the controller?

ActiveRecord will execute the query when its used

cars = Car.where(:colour => 'black') # No Query
cars.each {|c| puts c.name } # Fires "select * from cars where ..."

“Lazy Loading” - http://m.onkey.org/active-record-query-interface

August 20, 2013
1 thank

everything is ok

Olefine, I’m not sure here is a good place for such questions (better use stackoverflow for example), but answer for your question is that Rails provide slice (and many other methods) not only for Hash class but for HashWithIndifferentAccess too such as for any other superclass of Hash, so they use

hash = self.class.new

for a reason. {} is a literal only for Hash.

August 17, 2013
0 thanks

for finding content

it will use all the field related to particular table so you can find data by any table field like Table name => ABC(:id, :name, :address) if you want to find data related to id or name or address than only write




find_by_field_name will find only first data match with it

if u want to find all data than enter




August 15, 2013 - (v3.2.1 - v3.2.13)
0 thanks

expires_in option

You can actually pass in an expires_in option that sets how long Rails should show the fragment before deleting it so as an example …

<% cache('homepage_sidebar', :expires_in => 10.minutes) do %>
<% end %>

This only used to work with memcached but it now works with other types of Rails stores, MemoryStore, FileStore (had to use a plugin to get this behavior before) etc etc

So in your controller. You’d just do …

@posts = Posts.all if fragment_exists?('homepage_sidebar') 

to avoid performing a pointless SQL query.

August 12, 2013
0 thanks

unuseful code

Why it use?

hash = self.class.new

Instead it, we may use

hash = {}
August 7, 2013
0 thanks

Not Supported on Memcached.

Just an FYI, this is not supported on Memcached, so I would avoid it if you’re using FileStore and are planning on moving over to Memcached eventually.

July 25, 2013
0 thanks
July 23, 2013
1 thank

Use strings as parameters, not booleans

I just stumbled across this somewhere in our codebase. The first example is faulty, the second one is correct.

= f.check_box :public, {}, true, false
# <input id="event_public" name="event[public]" type="checkbox" value="true" />


= f.check_box :public, {}, "true", "false"
# <input name="event[public]" type="hidden" value="false" />
# <input id="event_public" name="event[public]" type="checkbox" value="true" />