views:

145

answers:

2

I have the following associated models

class Enrollment < ActiveRecord::Base
  has_many :addresses
end

class Address < ActiveRecord::Base
  belongs_to :address_type
end

Currently I'm using the following (which I think is ugly) to filter out enrollment addresses of a certain address type.

class Enrollment < ActiveRecord::Base
  def local_address
    adds = []
    addresses.each do |add| 
      adds << add if add.address_type.name == 'Local'
    end
    adds.last
  end
end

Is there a way of using named scope of doing the same thing?

A: 

A generic solution:

class Address < ActiveRecord::Base
  belongs_to :address_type
  named_scope :local, { :conditions => { :address_type => { :name => "Local" }}}
end

This allows you to do the following:

Enrollment.find(12).addresses.local  # Association extended with .local method
Address.local.all                    # Class methods extended with .local method

The named scope could help in all situations where you are only using "local" addresses.

molf
You solution gave the following error.Mysql::Error: Unknown column 'address_type.name' in 'where clause': SELECT * FROM `addresses` WHERE (`addresses`.enrollment_id = 8) AND ((`address_type`.`name` = 'Local') AND (`addresses`.enrollment_id = 8)) ORDER BY addresses.id DESC LIMIT 1Seems like we have to use join statements.
JasonOng
A: 

With reference from the following stackoverflow post, I managed to solved my named scope query

http://stackoverflow.com/questions/166217/rails-namedscopes-with-joins

Basically I need to do joins in the query

class Address < ActiveRecord::Base
  belongs_to :address_type
  named_scope :local, { 
    :joins => "INNER JOIN address_types ON address_types.id = addresses.address_type_id",
    :conditions => "address_types.name = 'Local'"
  }
end

So effectively I can rewrite my Enrollment's "local_address" method to

clss Enrollment < ActiveRecord::Base
    def local_address
      addresses.local.last
    end
end
JasonOng