multiple_table_inheritance icon indicating copy to clipboard operation
multiple_table_inheritance copied to clipboard

Using .inludes(:related_model) on Child model.

Open talifhani opened this issue 12 years ago • 8 comments

Hey little issue i came across.

class Item < ActiveRecord::Base
    acts_as_superclass
    has_many :rows
end

class Invoice < ActiveRecord::Base
    inherits_from :item
end

class Row < ActiveRecord::Base
    belongs_to :item
end

I found that .includes(:related_model) doesnt seem to work on the child model. Works on the parent of cause.

# The Below
class InvoicesController < ApplicationController
    def index
         # Get invoice invoices with all their rows. THIS DOESN'T WORK
        @invoices = Invoice.includes(:rows).all
    end
end

I get the error below.

Association named 'rows' was not found; perhaps you misspelled it?

talifhani avatar Jan 13 '13 14:01 talifhani

Check out the last section of the README named "Methods":

When inheriting from another parent model, methods can optionally be called on the parent model automatically as well. To do so, specify the :methods option when calling inherits_from.

So in your case, you'll want to do this:

class Invoice < ActiveRecord::Base
  inherits_from :item, methods: true
end

mhuggins avatar Jan 13 '13 15:01 mhuggins

Hey thanks for the quick response buddy. I Added

inherits_from :item, methods: true

Getting the error below. FYI i'm a total Rails n00b.

undefined method `key?' for nil:NilClass

I'm not even trying to do a .include(:related_model) call

talifhani avatar Jan 13 '13 18:01 talifhani

Can you include the whole stack trace?

mhuggins avatar Jan 13 '13 18:01 mhuggins

Hey Matt sorry was using two gems, had this confused with the 'class-table-inheritance' gem. Arrrrrg. So I removed it now im using multiple_table_inheritance. Still getting this error thought after adding :methods => true

Association named 'rows' was not found; perhaps you misspelled it?

talifhani avatar Jan 13 '13 18:01 talifhani

If you include the relevant code where you call .rows along with full stack trace, I might be able to help take a look.

mhuggins avatar Jan 13 '13 18:01 mhuggins

The error message also makes it sound like there might be an issue with the association on Item itself since that is a Rails error. Instead of directly calling invoice.rows, can you try calling invoice.item.rows to see if you get the same error?

mhuggins avatar Jan 13 '13 18:01 mhuggins

invoice.rows.each do |row| works just fine without .includes(:rows) but i'm trying to avoid the N+1 queries issue In the view.

Hey here is the code i have.

class InvoicesController < ApplicationController
    def index
                @invoices = Invoice.includes(:rows).all
    end
end

Also If I change

@invoices = Invoice.includes(:rows).all
# to
@invoices = Item.includes(:rows).all
# this Works
class Item < ActiveRecord::Base
    has_many :rows
    acts_as_superclass
end


class Invoice < ActiveRecord::Base
    inherits_from :item, :methods => true
    attr_accessible :header, :footer
end

class Estimate < ActiveRecord::Base
    inherits_from :item, :methods => true
    attr_accessible :header, :footer
end


class Row < ActiveRecord::Base
  belongs_to :item
  attr_accessible :cost, :description, :item
end

In the view.

<ul>
<% @invoices.each do |invoice| %> 
    <li><%= invoice.header %></li>
        <li>
        <ul>
              <% invoice.rows.each do |row| %>
            <li><%= row.cost %></li>
          <% end %>
        </ul>
        </il>
<% end %>
</ul>

Obviously this is just messing around for now.

talifhani avatar Jan 13 '13 19:01 talifhani

Bump. Sorry.

talifhani avatar Jan 18 '13 08:01 talifhani