Hi all !

I've been meaning to ask for a while now.  Please look at association_proxy:
http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/associations/association_proxy.rb#L105

Why are we only setting the primary key ?  Why aren't we also copying
the reference to the owner object in the record ?

The following fails:

class Recipient < AR::B
  belongs_to :account
  validates_presence_of :account_id
  before_validation {|r| r.account = r.email.account if r.email }
end

class Email < AR::B
  has_many :recipients
  belongs_to :account
end

email = Email.new(...)
email.recipients.build(...)
email.save!

ActiveRecord::RecordInvalid: Validation failed: Recipients is invalid

The problem is actually caused by the fact that AR doesn't copy the
owner object in the newly instantiated has_many.  Patching 1.2-stable
with the following:

Index: lib/active_record/associations/association_proxy.rb
===================================================================
--- lib/active_record/associations/association_proxy.rb (revision 5577)
+++ lib/active_record/associations/association_proxy.rb (working copy)
@@ -103,6 +103,8 @@
             record["[EMAIL PROTECTED]:as]}_type"] =
@owner.class.base_class.name.to_s
           else
             [EMAIL PROTECTED] = @owner.id unless
@owner.new_record?
+            record.send(@reflection.active_record.name.underscore +
"=", @owner) \
+                if record.is_a?(ActiveRecord::Base)
           end
         end

allows my test to run, but I get one AR failure:

  1) Error:
test_insert(TreeTest):
NoMethodError: undefined method `tree_mixin=' for #<TreeMixin:0xb6efbbc0>
    ./test/../lib/active_record/base.rb:1860:in `method_missing'
    ./test/../lib/active_record/associations/association_proxy.rb:106:in `send'
    ./test/../lib/active_record/associations/association_proxy.rb:106:in
`set_belongs_to_association_for'
    ./test/../lib/active_record/associations/has_many_association.rb:14:in
`build'
    ./test/../lib/active_record/associations/association_collection.rb:93:in
`create'
    ./test/mixin_test.rb:260:in `test_insert'

That's because I'm not finding the using reflection properly.  But no
matter, is it something we want in the code base ?

Thanks !
-- 
François Beausoleil
http://blog.teksol.info/
http://piston.rubyforge.org/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to