Tuesday, 10 April 2012

Finding null records from date datatype column in postgres database


Hi,

While working with Postgres I had faced issue with query with date data type. When I try to fetch records with date column as null as below

subscriptions = Subscription.where(:retry_date => ' ' )

I have got the below error

PG::Error: ERROR: invalid input syntax for type date:

I came to see we cannot find the records through Active record as above.

By googling I found the way to fetch the records as below

subscriptions = Subscription.where(:retry_date => NIL )

Thank You,
Uma Mahesh.

Wednesday, 4 April 2012

Error on Devise Registrations controller spec


Hi,

When I run the registrations controller spec file to test the registration page of the devise. I am getting the below error

ruby -I spec spec/controllers/registrations_controller_spec.rb
F

Failures:

  1) RegistrationsController Display Registration page returns http success
     Failure/Error: get 'new'
     AbstractController::ActionNotFound:
       Could not find devise mapping for path "/users/sign_up".
       Maybe you forgot to wrap your route inside the scope block? For example:
     
       devise_scope :user do
         match "/some/route" => "some_devise_controller"
       end
     # spec/controllers/registrations_controller_spec.rb:7:in `block (3 levels) in <main>'

Finished in 0.2411 seconds
1 example, 1 failure

Failed examples:

rspec spec/controllers/registrations_controller_spec.rb:6 # RegistrationsController Display Registration page returns http success

running specific spec file through console

Here is the command:

> ruby -I spec spec/models/user_spec.rb

rails generate


rails generate
Usage: rails generate GENERATOR [args] [options]

General options:
  -h, [--help]     # Print generator's options and usage
  -p, [--pretend]  # Run but do not make any changes
  -f, [--force]    # Overwrite files that already exist
  -s, [--skip]     # Skip files that already exist
  -q, [--quiet]    # Suppress status output

Please choose a generator below.

Rails:
  assets
  controller
  generator
  helper
  inherited_resources_controller
  integration_test
  mailer
  migration
  model
  observer
  performance_test
  resource
  responders_controller
  scaffold
  scaffold_controller
  session_migration
  task

ActiveAdmin:
  active_admin:assets
  active_admin:devise
  active_admin:install
  active_admin:resource

ActiveRecord:
  active_record:devise

Coffee:
  coffee:assets

Cucumber:
  cucumber:install

Devise:
  devise
  devise:install
  devise:views

EmailSpec:
  email_spec:steps

Erb:
  erb:controller
  erb:mailer
  erb:scaffold

FactoryGirl:
  factory_girl:model

Formtastic:
  formtastic:form
  formtastic:install

Jquery:
  jquery:install

Js:
  js:assets

Kaminari:
  kaminari:config
  kaminari:views

Mongoid:
  mongoid:devise

Paperclip:
  paperclip

Responders:
  responders:install

Rspec:
  rspec:controller
  rspec:helper
  rspec:install
  rspec:integration
  rspec:mailer
  rspec:model
  rspec:observer
  rspec:scaffold
  rspec:view

TestUnit:
  test_unit:controller
  test_unit:helper
  test_unit:integration
  test_unit:mailer
  test_unit:model
  test_unit:observer
  test_unit:performance
  test_unit:plugin
  test_unit:scaffold

Wednesday, 21 March 2012

bootstrap plugin collapse, collapsible components like accordions and navigation in jquery


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