mhuggins / multiple_table_inheritance

Multiple Table Inheritance is a plugin designed to allow for multiple table inheritance between your database tables and your ActiveRecord models.
30 stars 4 forks source link

Using .inludes(:related_model) on Child model. #11

Open talifhani opened 11 years ago

talifhani commented 11 years ago

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?
mhuggins commented 11 years ago

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
talifhani commented 11 years ago

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

mhuggins commented 11 years ago

Can you include the whole stack trace?

talifhani commented 11 years ago

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?
mhuggins commented 11 years ago

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 commented 11 years ago

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?

talifhani commented 11 years ago

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 commented 11 years ago

Bump. Sorry.