On Jan 25, 2015, at 3:42 PM, Maxim Chernyak <madfanc...@gmail.com> wrote:

> When using STI, if you introduce a new subclass in a git branch, then create 
> a db record while in it, then switch to another branch, you will get 
> uninitialized constant error when trying to query those records. Certain 
> kinds of applications (like ours) could run into this a lot, so I'm wondering 
> if there could be a null-object-patternish solution to this. E.g:
> 
> * Accept proc for inheritance_column, which evaluates on instances, with type 
> passed as an arg to it, and whatever it returns becoming the record in that 
> case. This way we can plug those records with some placeholders

The proc can’t be run on an instance - the error is arising when AR tries to 
determine what class to use. The calling context is in find_sti_class:

https://github.com/rails/rails/blob/6d72485b6977804a872dcfa5d7ade2cec74c768e/activerecord/lib/active_record/inheritance.rb#L169

a class method defined on ActiveRecord::Base. At runtime, this will have access 
to the class that `find` was called on (presumably the STI base class) and the 
value from the inheritance column.

Returning a fully-initialized object from here would require skipping a bunch 
of code in `instantiate`.

> * Add option self.ignore_unknown_types = true to activerecord to simply 
> auto-rescue + skip records with uninitialized constant error (perhaps print a 
> warning instead)

It’s not clear to me what “skipping” a record in this scenario would mean. If I 
have a model that belongs_to an instance of the missing class, what do I get 
back when I access it? `nil`? `RecordNotFound` exception? If I have a 
validation that requires the presence of that missing object, does `save` still 
work?

There are plenty more questions like those. “Muddling through” with bad data is 
almost certainly going to break something upstream in people’s applications - 
something considerably harder to troubleshoot than a straightforward 
`SubclassNotFound` error on loading a bad record.

As to the “sample data” issue, if it’s worth keeping in the DB it’s worth 
keeping in source control. If writing a db/seeds.rb file is too complicated 
(maybe you’re importing large datasets from elsewhere), I’d recommend things 
like YamlDb to make repeatable seed data. Even `mysqldump > sample_data.sql` is 
a start…

—Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail

Reply via email to