0

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?

molf
  • 73,644
  • 13
  • 135
  • 118
JasonOng
  • 3,268
  • 3
  • 21
  • 17

2 Answers2

0

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
  • 73,644
  • 13
  • 135
  • 118
  • 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 1 Seems like we have to use join statements. – JasonOng Jul 28 '09 at 02:41
0

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

Rails named_scopes 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
Community
  • 1
  • 1
JasonOng
  • 3,268
  • 3
  • 21
  • 17