Created
May 8, 2012 14:38
-
-
Save venkatd/2635673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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