Notes posted to Ruby on Rails

RSS feed
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
0 thanks

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" />
July 18, 2013 - (v3.2.1 - v3.2.13)
0 thanks

Nested i18n attributes

If you want to use nested attributes in a i18n file (like person :has_many => :addresses), write:

        street: "Street name"
        area: "Area code"
        number: Number
July 16, 2013 - (>= v3.2.1)
0 thanks

Difference between fullpath

From what I’ve seen, it looks like the difference between this and #fullpath is that this method doesn’t include parameters that weren’t in the original url (i.e. parameters that were sent via POST instead of GET).

July 16, 2013
0 thanks

isolate_namespace description with example

Normally when you create controllers, helpers and models inside an engine, they are treated as if they were created inside the application itself. This means that all helpers and named routes from the application will be available to your engine’s controllers as well.

However, sometimes you want to isolate your engine from the application, especially if your engine has its own router. To do that, you simply need to call isolate_namespace. This method requires you to pass a module where all your controllers, helpers and models should be nested to:

module MyEngine
 class Engine < Rails::Engine
   isolate_namespace MyEngine


With such an engine, everything that is inside the MyEngine module will be isolated from the application.

Detail reference: http://edgeapi.rubyonrails.org/classes/Rails/Engine.html

July 12, 2013 - (v3.0.0 - v3.2.13)
0 thanks

Accept header ignored

Rails ignores the accept header when it contains “,/” or “/,” and returns HTML (or JS if it’s a xhr request).

This is by design to always return HTML when being accessed from a browser.

This doesn’t follow the mime type negotiation specification but it was the only way to circumvent old browsers with bugged accept header. They had he accept header with the first mime type as image/png or text/xml.

July 11, 2013
0 thanks

Using fields_for with collection (no association)

accepts_nested_attributes_for has some detractors: http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models

But fields_for is defined both on FormBuilder and FormHelper and is still useful even when accepts_nested_attributes_for is not being used. Consider a “to do list” application where we wish to edit and update a list of tasks (Task model). I like to use a TaskCollectionController to manage the actions on the collection, it feels more RESTful than overloading a TaskController:

class TaskCollectionController < ApplicationController
# task_collection_edit GET /task_collection/edit 
  def edit
    @tasks = Task.all

# task_collection_update PUT /task_collection/update
  def update
    # it would be better to use a TaskCollection form object here
    # to make this controller skinny,
    # for the sake of brevity I skipped the form object
    params[:tasks].values.each do |attrs|
      if attrs[:_destroy] == '1'
      elsif attrs[:id].blank?
    redirect_to task_collection_edit_path

In the edit view, I use fields_for to index added tasks, and it also will insert the id as a hidden field for existing tasks:

%h1 Edit Task Collection
= form_tag task_collection_update_path, :method => :put do
      %th Name
      %th Priority
    - @tasks.each do |task|
      = fields_for 'tasks[]', task, :hidden_field_id => true do |task_form|
        = render :partial => 'task_form', :locals => {:task_form => task_form}

  = button_tag 'Save'

= button_tag "Add task", :type => :button, :id => :add_task

%script{:type => 'html/template', :id => 'task_form_template'}
  = fields_for 'tasks[]', Task.new, :index => 'NEW_RECORD', :hidden_field_id => true do |task_form| render(:partial => 'task_form', :locals => {:task_form => task_form}); end

    task_form = function(){ return $('#task_form_template').text().replace(/NEW_RECORD/g, new Date().getTime())}
    var add_task = function(){ $('#tasks').append(task_form()) }
    $('#add_task').on('click', add_task)

:hidden_field_id => true triggers the insertion of the id field, and a placeholder index “NEW_RECORD” is replaced by javascript when a task is added, as others have described here. When there’s no association, the index key is :index, vs. :child_index in the case of an association.

Here’s the partial used in the edit view:

  %td= task_form.text_field :name
  %td= task_form.select :priority, [1,2,3,4,5]
  %td= remove_item(task_form)

The remove_item helper triggers the deletion of persisted tasks:

module TaskCollectionHelper
  def remove_item(form_builder)
    if form_builder.object.new_record?
      # If the task is a new record, remove the div from the dom
      link_to_function( 'Remove', "$(this).closest('tr').remove()")
      # However if it's a "real" record it has to be deleted from the database,
      # hide the form and mark for destruction
      form_builder.hidden_field(:_destroy) +
      link_to_function( 'Remove', "$(this).closest('tr').hide(); $(this).siblings().attr('value',1)")
June 26, 2013 - (v3.0.0 - v3.2.13)
0 thanks



# SELECT `users`.* FROM `users` WHERE (`users`.`id` != 1) AND (`users`.`name` IS NOT NULL)
 User.where.not(id: 1).where.not(name: nil)
June 26, 2013 - (v3.2.13)
0 thanks

Typing mismatch

This block

if size = options.delete(:size)
  options[:width], options[:height] = size.split("x") if size =~ %{^\d+x\d+$}

has type mismatch

June 19, 2013 - (>= v3.2.13)
0 thanks

Must use :class, not 'class'

Note that

<%= content_tag_for(:li, @person, :class => “bar”) %>

does the right thing.

<%= content_tag_for(:li, @person, ‘class’ => “bar”) %>

will not!

June 17, 2013
0 thanks

Use dom_id( resource_instance ) to create the HTML id

In the notes on this page people use:


But you can use this function in stead:

June 5, 2013 - (v2.0.3 - v3.2.13)
0 thanks

Html inside Lable tag

I need this

   <select size="1" name="dyntable_length" aria-controls="dyntable">
     <option value="10" selected="selected">10</option>
     <option value="25">25</option>
     <option value="50">50</option>
     <option value="100">100</option>


I made a helper method:

def entries_lablel()
   label_tag '' do
     concat 'Show '
     concat content_tag(:select, options_for_select([10, 25, 50, 100]),
       {name: 'dyntable_length', size: 1}
     concat ' entries'

and In my html.erb file I called it

<%= entries_lablel %>

You can pass paramateres to make it more generic also You can add multiple select elements or any other element using the same

May 31, 2013
1 thank

Do not mistakenly use serialize like other similar directives - attr_accessible, attr_accessor

serialize seems very similar to other directives that work on attributes such as attr_accessible. One may mistakenly assume that serialize can take a list of attributes. For eg:

class Tuk < ActiveRecord::Base
  attr_accessible :foo, :bar
  serialize :foo, :bar

This may lead to a cryptic error. Eg.

puts !Tuk.first.foo.nil?


NoMethodError at /file:location undefined method `new' for :bar:Symbol

This is because it tries to parse the YAML string stored in foo as an instance of :bar.

May 30, 2013
0 thanks

Avoiding to_param method when using URL helper methods

I recently found myself in the situation where I needed to generate URLs which included the ID instead of the value returned from the model’s to_param method (since someone had overridden the to_param method). It turned out to be easier than I thought. You can simply pass an ID to the helper method and it will construct the URL correctly:

# /admin/foobars/123/edit
May 23, 2013 - (v3.0.0 - v3.2.13)
0 thanks

Use @output_buffer to set the context.

You can use assert_select to test helpers, just have to set the @output_buffer before you do.

Code example

class CurrencyHelperTest < ActionView::TestCase

  setup do
    # can use helper methods here
    @output_buffer = currency 54.78

  test 'currency use a div' do
    asert_select 'div'

May 23, 2013
0 thanks

A simple usage example

See http://apidock.com/rails/String/inquiry

env = "production".inquiry
env.production?  # => true
env.development? # => false
May 18, 2013
0 thanks

:include is also valid option

my_company.serializable_hash(:include => [:people])

May 15, 2013
0 thanks
April 27, 2013 - (v3.1.0 - v3.2.13)
2 thanks
April 10, 2013 - (v3.2.1 - v3.2.13)
2 thanks

be aware that this writes to tmp/cache

Its supposed to be http caching, but Rails will actually cache the response to whatever you specified as the cache store, *as well*, but only if you specify :public => true. The default is filestore so it will try to write to tmp/cache.

Only a problem if you don’t have the proper permissions set, in that scenario your apache/nginx logs could fill up very quickly with “permission denied errors”

Full explanation is here http://blog.tonycode.com/archives/418

April 4, 2013 - (>= v1.0.0)
0 thanks

increment_by_sql for PG

Note, if you’re using the code below for incrementing by SQL with a Postgres database, it’s not going to like the backticks. Just remove them:

def increment_with_sql!(attribute, by = 1)
  raise ArgumentError("Invalid attribute: #{attribute}") unless attribute_names.include?(attribute.to_s)
  original_value_sql = "CASE WHEN #{attribute} IS NULL THEN 0 ELSE #{attribute} END"
  self.class.update_all("#{attribute} = #{original_value_sql} + #{by.to_i}", "id = #{id}")
April 2, 2013 - (v3.0.0 - v3.2.13)
0 thanks

Use concat insted of joining collection explicitely

concat method will be useful to join the collection object from looping conditions.

arr = ["a", "b", "c"]
content_tag(:ul, :class => 'a class') do
  arr.each do |item|
  concat content_tag(:li, item)

And this will generate the html as shown below

<ul class="a class">