Fix STI descentants by constantizing models #9935

Closed
wants to merge 1 commit into
from

Projects

None yet

2 participants

@dfens
Contributor
dfens commented Mar 26, 2013

Assume we've got three models:

class Payment < ActiveRecord::Base
class CardPayment < Payment
class CreditCardPayment < CardPayment

Assume that at least one CreditCardPayment have been added to database.

Run console:

>> CardPayment.last
  CardPayment Load (0.2ms)  SELECT "payments".* FROM "payments" WHERE "payments"."type" IN ('CardPayment') ORDER BY "payments"."id" DESC LIMIT 1
=> nil
>> CardPayment.descendants
=> []
>> CreditCardPayment.last
  CreditCardPayment Load (0.5ms)  SELECT "payments".* FROM "payments" WHERE "payments"."type" IN ('CreditCardPayment') ORDER BY "payments"."id" DESC LIMIT 1
=> #<CreditCardPayment id: 1, type: "CreditCardPayment", created_at: "2013-03-26 15:19:08", updated_at: "2013-03-26 15:19:08">
>> CardPayment.descendants
=> [CreditCardPayment(id: integer, type: string, created_at: datetime, updated_at: datetime)]

So the middle sti model CardPayment does not have descendants till CreditCardPayment will be called. Problem is fixed when preloaded , what do you think?

@fxn
Member
fxn commented Mar 26, 2013

AR is lazy you cannot just eager load the models.

One of the possible ways to solve this is by calling require_dependency in selected files of the STI hierarchy.

@fxn fxn closed this Mar 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment