multiple_table_inheritance
multiple_table_inheritance copied to clipboard
Using .inludes(:related_model) on Child model.
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?
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 callinginherits_from
.
So in your case, you'll want to do this:
class Invoice < ActiveRecord::Base
inherits_from :item, methods: true
end
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
Can you include the whole stack trace?
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?
If you include the relevant code where you call .rows
along with full stack trace, I might be able to help take a look.
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?
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.
Bump. Sorry.