Q:

Can I call a class method from another model in Rails?

I'm trying to call a class method (currently a scope) that uses an attribute from its parent (or belongs_to) model, but can't seem to get it working right.
My models:
class Venue < ActiveRecord::Base
  attr_accessible :address
  has_many :events, :dependent => :destroy
end

class Event < ActiveRecord::Base
  belongs_to :venue
  scope :is_near, lambda {|city| self(Venue.address).near(city, 20, :units => :km)}
end

I know the syntax is wrong, but I think that illustrates what I'm intending to do. I want to get the address of the venue and call another method on it. I need the scope in the Event class so I can chain other scopes together.
Appreciate any ideas.

A:

Since #address is not a class method but an instance method, you won't be able to do what you want by using a scope.
If you want to get all the events within a 20km range of a venue, create these class methods in Venue instead:
class Venue < ActiveRecord::Base
  def self.events_near_city(city)
    venues_near_city(city).map(&:events).flatten
  end

  private

  def self.venues_near_city(city)
    near(city, 20, :units => :km)
  end
end

Then call it by using Venue.events_near_city(session[:city]) since, as you told me in chat, you're storing the city in the session.

