extract_options! usage


In one of my method in my code, I have seen the method "extract_options!"

 def fields_for(record_name, *args, &block)
      options = args.extract_options!
-----
-----
-----
 end

Its mainly used for removing the hash from the array from the last postion if exists and return the array.


Thank You,
Uma Mahesh

Tuesday, 20 March 2012

html to haml


Hi,

Here is the nice web interface where you can convert the html code to haml simply by button click.

URL: http://html2haml.heroku.com/

Thank you,
Uma Mahesh.

applying css class for country_selec tag.


Hi,

I am using country_select tag for displaying the list of all countries in the dropdown. I have faced issue for applying css class to fix the width for the slect box.

I have tried passing the html options as below
= f.country_select(:country, {:class => "span2", :id => "select01"})

But select box was not effected with style. After making some googling found the solution. Below is the solution.

f.country_select(:country, {},{}, {:class => "span2", :id => "select01"})

Need to pass two blank hashes. The fourth parameter is the html options parameter for country_select tag.

Thank You,
Uma Mahesh.

Wednesday, 7 March 2012

usage of "changed_attributes" method in active record


Hi,

changed_attributes method provides a useful functionality to check the value that was updated in a column in before the object being saved.

These methods available under the "Active Model Dirty"

Here is the clear explanation on it.


person = Person.find_by_name('Uma Mahesh')
person.changed?       # => false

person.name = 'Varma'
person.changed?       # => true
person.name_changed?  # => true
person.name_was       # => 'Uma Mahesh'
person.name_change    # => ['Uma Mahesh', 'Varma']


A clear explanation of these methods were explained in the url :



http://api.rubyonrails.org/classes/ActiveModel/Dirty.html#method-i-changed_attributes
This method can be used to check the column value was updated or not. If you want to check the specific column value was update or not, this can be done by as below. 

In your model:

before_update :update_image_count_counter


  def update_image_count_counter
    if changed_attributes.keys.include?("image_count")
       # your required code comes here
    end
  end



Thank You,
Uma Mahesh