This project is archived and is in readonly mode.

#5249 ✓resolved
gucki

active_record does not follow active_model specs for #to_key

Reported by gucki | July 30th, 2010 @ 10:00 AM | in 3.0.2

I hit this while playing with sequel together rails rails 3.0rc. Please see #to_key here:

http://github.com/rails/rails/blob/master/activemodel/lib/active_mo...
http://github.com/rails/rails/blob/master/activerecord/lib/active_r...

AR is using #new_record? while AM is using #persisted? to check wether to return an id.

Consider the following code:
@news = News.create @news.destroy dom_id(@news)

As sequel conforms to the active_model specs the following returns "new_news".
ActiveRecord does not follow the AM specs and so returns "new_4545".

I think the implementation in AM is wrong and AR is correct. Otherwise a lot of old rails apps won't work anymore, because you destroy the @news in den controller and call afterwards in a template something like $('##{dom_id @news}').hide() which won't work if you check for persisted? because persisted? is false (in AR and Sequel) after the object has been destroyed.

Example using AR:

ruby-1.9.2-head > a = News.create
=> # 
ruby-1.9.2-head > a.new_record?
=> false 
ruby-1.9.2-head > a.persisted?
=> true 
ruby-1.9.2-head > a.destroyed?
=> false 
ruby-1.9.2-head > ApplicationController.new.dom_id a
=> "news_2" 
ruby-1.9.2-head > a.destroy
=> # 
ruby-1.9.2-head > a.new_record?
=> false 
ruby-1.9.2-head > a.persisted?
=> false 
ruby-1.9.2-head > a.destroyed?
=> true 
ruby-1.9.2-head > ApplicationController.new.dom_id a
=> "news_2"

Using Sequel (or any other ORM which conforms to AM):

ruby-1.9.2-head > a = News.create
 => #87363, :user_id=>nil, :recipient_id=>0, :ref_id=>nil, :ref_type=>"Messagein", :created_at=>nil, :notification=>0}> 
ruby-1.9.2-head > a.new?
 => false 
ruby-1.9.2-head > a.persisted?
 => true 
ruby-1.9.2-head > ApplicationController.new.dom_id a
 => "news_87363" 
ruby-1.9.2-head > a.destroy
 => #87363, :user_id=>nil, :recipient_id=>0, :ref_id=>nil, :ref_type=>"Messagein", :created_at=>nil, :notification=>0}> 
ruby-1.9.2-head > a.new?
 => false 
ruby-1.9.2-head > a.persisted?
 => false 
ruby-1.9.2-head > ApplicationController.new.dom_id a
 => "new_news" 

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages