This project is archived and is in readonly mode.

#3383 ✓resolved
Paul Jensen

Nested attributes for ignores default scope on child model

Reported by Paul Jensen | October 16th, 2009 @ 01:55 PM

When you setup a nested form for a parent model, the default scope for the child models are ignored.

E.g.

class Package < ActiveRecord::Base
  has_many :products
  accepts_nested_attributes_for :products
end
class Product < ActiveRecord::Base
  belongs_to :package
  default_scope :order => 'end_date ASC'
end

In the view:

<% form_for @package, :url => update_all_admin_package_products_path(@package), :html => {:method => :put} do |f| %>
  <% f.fields_for :products do |product_form| %>
     <td><%= product_form.date_select :end_date %></td>
  <% end %>
<% end %>

If you create two products:

< Product id:1, end_date: 2nd February 2010 >, < Product id:2, end_date: 1st February 2010 >

You would expect the products to be displayed in this order (sorted by end date ascending):

< Product id:2, end_date: 1st February 2010 >, < Product id:1, end_date: 2nd February 2010 >

However, they are sorted by id ascending.

In order to get the package's products to be sorted by the end date ascending, I removed the default scope (as it was redundant), and added the following conditions on the relationship between package and product:

class Package < ActiveRecord::Base
  has_many :products, :order => 'end_date ASC'
  accepts_nested_attributes_for :products
end

Maybe I'm making the wrong assumption, but should nested form attributes inherit the default scope of the child model?

I'm using Rails 2.3.4 on Mac OS X Snow Leopard, ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]

Comments and changes to this ticket

  • Eloy Duran

    Eloy Duran October 16th, 2009 @ 02:17 PM

    • Tag changed from activerecord, default_scope, nested_attributes_for to activerecord, default_scope
    • State changed from “new” to “incomplete”

    It simply iterates over the association collect as it receives it from the model. So if you'd expect it to return that collection with the default scope applied, then that's what should be fixed. Ie, it's not a nested attributes issue. Could you try to isolate the issue (a failing test for the AR test suite) and edit this ticket, or create a new ticket?

    I have no opinion on whether or not the default scopes should apply to these collections, I don't use them.

  • Paul Jensen

    Paul Jensen October 16th, 2009 @ 10:34 PM

    • Assigned user cleared.

    Hi Eloy,

    Thanks for looking at the ticket, and pointing me in the right direction. My assertion is that a default scope should filter through to all collections of a class. I must confess I'm an absolute rookie to adding tests and patches to Rails, but I'll attempt to write a test and a patch for this, and reply here.

    Thanks again.

  • Mike Riley

    Mike Riley July 29th, 2010 @ 04:29 PM

    • State changed from “incomplete” to “resolved”
    • Importance changed from “” to “”

    Hello Paul,

    This ticket is quite old, but still wanted to check in on it. Were you able to write a test and a patch for this issue? I am going to mark this as resolved, but if you have more information or a failing test/patch we can reopen this and investigate further.

    Mike Riley

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>

Pages