Saari Development

Ali Rizvi's Technical Blog as a Professional Software Development Engineer

Archive for the ‘rails’ Category

Rails: Non-DB field in Model with validation

leave a comment »

Challenge: Add a field to the model representing system date, time & timezone that is not stored in the database and have validation on it.

Solution:

  • Add attr_accessor & attr_accessible (for bulk update), add getter (sys_date_time) & setter (sys_date_time=) methods.
  • Return system date time in UTC from getter and show in user (browser) timezone on client side (using moment-timezone.js which is moment.js extension select the download with timezone data)
  • When the setting come to be saved using update_attributes in controller it eventually calls model setter (sys_date_time=) where you convert it back to utc given the timezone info (ActiveSupport::Timezone[zone] is your friend with ActiveSupport::TimeZone[zone].parse then in_time_zone(‘UTC’))
  • Validation is tricky as this is not a db backed and getter returns the calculated value which is always valid. You need to validate input first thing in setter and set a new member variable with errors found. Then add a validate do … end block where you iterate through the new member variable with errors and do errors.add(attr, message).

Will add code snippets when time permits.

 

Advertisements

Written by imsaar

April 7, 2016 at 2:02 pm

Posted in code, rails, ruby

Rails: Skipping Base Class Filters in Subclasses

with 3 comments

I encountered a case where we needed to skip a filter as it was causing infinite redirection in a subclass. I found my answer in: Rails Filters Documentation

Filter chain skipping

Declaring a filter on a base class conveniently applies to its subclasses, but sometimes a subclass should skip some of its superclass’ filters:

  class ApplicationController < ActionController::Base
    before_filter :authenticate
    around_filter :catch_exceptions
  end

  class WeblogController < ApplicationController
    # Will run the :authenticate and :catch_exceptions filters.
  end

  class SignupController < ApplicationController
    # Skip :authenticate, run :catch_exceptions.
    skip_before_filter :authenticate
  end

  class ProjectsController < ApplicationController
    # Skip :catch_exceptions, run :authenticate.
    skip_filter :catch_exceptions
  end

  class ClientsController  :index
  end

Written by imsaar

November 10, 2009 at 9:07 pm

Posted in code, documentation, rails, ruby

MySQL and Rails: mysql.sock not found

leave a comment »

I started doing my webapp on my brand new server (ubuntu linux 7.04) and when trying to run my first migration I got the following error message:


rake db:migrate
(in /home/rizvi/projects/somewebapp)
rake aborted!
No such file or directory - /tmp/mysql.sock

Here is a useful mysql command to find out mysql defaults:


mysqladmin --print-defaults
mysqladmin would have been started with the following arguments:
--port=3306 --socket=/var/run/mysqld/mysqld.sock

Here is what solved the problem:


sudo apt-get install libmysql-ruby

Note: The above is an alternative to :

sudo gem install mysql

Now I get:


rake db:migrate
(in /home/rizvi/projects/somewebapp)
rake aborted!
Unknown database 'somewebapp_development'
(See full trace by running task with --trace)

So I need to do the following:


mysqladmin -u root create somewebapp_development -p
mysqladmin -u root create somewebapp_test -p
mysqladmin -u root create somewebapp_production -p

Reference: RailsOnUbuntu

Written by imsaar

August 6, 2007 at 6:37 am

Posted in mysql, rails