ActiveRecord create_or_update based on natural-key

Have you often run into a situation where you have a hash full of properties and you want to either create a new ActiveRecord object (if it doesn't exist) or update one (if it exists)?

For example,

class User
  # has properties: ssn, first_name, :last_name, :age, :email, :password
  def create_or_update_by_ssn(params)
    user = Disease.find_by_ssn(params[:ssn])
    if user.nil?
      user = User.create(params)
    else
      user.update_attributes!(params)
    end
  end
end

Here, the business rule states that a user's SSN is unique, so called natural-key.

If we can identify what constitutes a natural-key for each model object in our domain-model, when we could DRY out this functionality.

Continue reading »

acts_without_database: Leverage ActiveRecord with Non-Database Backed Objects

ActiveRecord comes with a lot of nice things that aren't really dependent on having a database backed model. The most obvious example of this is the validation framework baked into ActiveRecord. Also, there are several plugins which add some useful behavior to ActiveRecord objects but don't rely on having a database.

In the future, I believe the rails team aims to make certain things more modular and easier to pull out and use separately from ActiveRecord (validation being one of those). However, if you are working on a project which is locked into a certain version of rails or you're impatient, there is a very simple plugin which can solve your needs.

Enter acts_without_database...
Continue reading »

Mass Assigning Inheritance Column Values for ActiveRecord STI with Rails

One of the security features in Rails is to prevent mass assigning values for certain columns (when passing a hash to #new, #attributes=, and #update_attributes).  This is to ensure that a malicious user can't exploit the system by passing in values for certain attributes you really don't want them to change.

In Rails 2.1, one of the attributes that will always be protected from mass assignment is the inheritance_column.  In most cases, protecting inheritance_column; is desired because we don't want (for example) someone to change their user type by specifying type = 'Admin' when posting a form.  Despite this, there are some cases where we do want to allow the user to specify the type.

However, even if we specify #attr_accessible on the inheritance_column, we will not be able to mass assign a value to it.  Rails will not allow it unless we put in a little hack to work around this feature.

Continue reading »

How to use will_paginate with non-ActiveRecord collection/array

will_paginate is very well designed plugin. Besides ActiveRecord object integration, it can integrate with array and any collection that you may have. The README.rdoc (in version 2.2.2) and wiki clearly and concisely document how to use it with ActiveRecord objects. I recently needed to use it for a collection outside of activerecord and here is how I did it.
Continue reading »

About Pathfinder

Follow the Blog

    Get a monthly update on best practices for delivering successful software.

    Subscribe via email

      

    Subscribe via RSS      RSS icon

Topics

Search

WordPress

Comments about this site: info@pathf.com