public

  • Download Gist
active_record_association_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
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
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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
user_location.rb
Ruby
1 2 3 4
class UserLocation < ActiveRecord::Base
geocoded_by nil
belongs_to :user
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.