Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Issues with eager loading #1

Closed
aaronchi opened this issue Apr 30, 2010 · 4 comments
Closed

Issues with eager loading #1

aaronchi opened this issue Apr 30, 2010 · 4 comments

Comments

@aaronchi
Copy link

I'm having problems with this gem and nested fields. When rails queries the object that's loaded into a fields_for it comes back with the association class instead of the model class.

I think that this may be related to what the fields_for_nested_model function is getting back when it calls object.to_model on the association.

@ernie
Copy link

ernie commented Apr 30, 2010

Can you provide sample code to reproduce the problem?

@ernie
Copy link

ernie commented Apr 30, 2010

Additionally, can you confirm this issue is with metawhere, and not metasearch? The emphasis on the view makes it sound more like a metasearch ticket.

@aaronchi
Copy link
Author

Yes. I had meta_search installed and the error only happens if I add meta_where. Additionally, the problem happens with just meta_where installed.

The issue is pretty simple to duplicate. Just create a form with nested_fields on a model that accepts_nested_attributes_for

class Contact < ActiveRecord::Base
    has_many :phone_numbers, :as => :owner, :dependent => :destroy
    accepts_nested_attributes_for :phone_numbers, :allow_destroy => true
end

<%= form_for @contact do |f| %>
    <%= f.fields_for :phone_numbers do |pf| %>
      <%= pf.text_field :number %>
    <% end %>
<% end %>

The error I get here is:
undefined method `persisted?' for #Class:0x6da6028

But the real problem is that it's returning the wrong class for the object.

The class returned is ActiveRecord::Associations::HasManyAssociation instead of the PhoneNumber model

@ernie
Copy link

ernie commented Apr 30, 2010

Thanks for the clarification. I found the problem. There are two solutions at this point:

  1. Remove the definition of Symbol#=~ in core_ext/symbol.rb
  2. Apply the patch to Rails I just put in at https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/4513

More details are there.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants