Skip to content

Instantly share code, notes, and snippets.

@venkatd
Created May 8, 2012 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save venkatd/2635673 to your computer and use it in GitHub Desktop.
Save venkatd/2635673 to your computer and use it in GitHub Desktop.
require 'spec_helper'
describe "ActiveRecord" do
describe "association" do
it "should point to the same object" do
user = create(:user)
user.current_location.should == nil
user.update_location(latitude: 11, longitude: 22)
user.current_location.should_not == nil
location = UserLocation.first
location.id.should == user.current_location.id
location.object_id.should == user.current_location.object_id #fails on this line
end
end
end
class User < ActiveRecord::Base
has_many :locations, :class_name => 'UserLocation', :order => 'created_at DESC'
has_one :current_location, :class_name => 'UserLocation', :conditions => {:is_active => true}
has_many :past_locations, :class_name => 'UserLocation', :conditions => {:is_active => false}, :order => 'created_at DESC'
def clear_location
unless current_location.nil?
current_location.is_active = false
current_location.save
reload
end
end
def self.clear_all_locations
UserLocation.where(:is_active => true).each do |user_location|
user_location.user.clear_location
end
end
end
class UserLocation < ActiveRecord::Base
geocoded_by nil
belongs_to :user
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment