byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
439 stars 57 forks source link

Rails 4.2 Test Crash #29

Closed CyborgMaster closed 9 years ago

CyborgMaster commented 9 years ago

I just upgraded our project to Rails 4.2 and now I'm getting the following crash when creating objects in our tests. I'm still digging into it, but I thought I would post here in case anyone had seen this before.

NoMethodError: undefined method `cast_type' for nil:NilClass
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-typedstore-0.5.1/lib/active_record/typed_store/extension.rb:132:in `coder_for'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-typedstore-0.5.1/lib/active_record/typed_store/extension.rb:142:in `write_attribute'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:80:in `replace_keys'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-4.2.0/lib/active_record/associations/belongs_to_association.rb:14:in `replace'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-4.2.0/lib/active_record/associations/singular_association.rb:17:in `writer'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/gems/activerecord-4.2.0/lib/active_record/associations/builder/association.rb:123:in `patient='
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/lathe.rb:56:in `assign_attribute'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/lathe.rb:16:in `block in initialize'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/lathe.rb:16:in `each'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/lathe.rb:16:in `initialize'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/blueprint.rb:24:in `new'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/blueprint.rb:24:in `make'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/active_record/blueprint.rb:6:in `make!'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/machinable.rb:52:in `block in make!'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/machinable.rb:88:in `decode_args_to_make'
/Users/jeremy/.rvm/gems/ruby-2.0.0-p451@allynova/bundler/gems/machinist-bebd6491b2ee/lib/machinist/machinable.rb:50:in `make!'
byroot commented 9 years ago

That's interesting. It probably mean the schema wasn't loaded yet, might be related to machinist.

If you manage to have a small repro app, I'll make a pleasure to dig into this.

CyborgMaster commented 9 years ago

Thanks for the offer to help, no example app so far. I'll let you know if I end up putting one together.

It looks like it might be an interaction with composite_primary_keys. The stack trace shows up when writing an association and when I patch the coder_for method to print the attribute name like so:

      def coder_for(attr_name)
        ap attr_name.to_s
        column = self.class.columns_hash[attr_name.to_s]
        return unless column.cast_type.is_a?(::ActiveRecord::Type::Serialized)
        column.cast_type.coder
      end

I got the following right before the crash:

"[:patient_id, :source_id]"
CyborgMaster commented 9 years ago

I'm not completely sure yet (still dealing with some weird test failures, but I think they are unrelated), but changing coder for like follows seems to work:

      def coder_for(attr_name)
        column = self.class.columns_hash[attr_name.to_s]
        return unless column && column.cast_type.is_a?(::ActiveRecord::Type::Serialized)
        column.cast_type.coder
      end

An extra existence check may be all we need

byroot commented 9 years ago

Right, so it's basically the same mistake that 9103a3a1892fc5c2f152da06c29b54a0f1fff60c fixed. In some circumstances the columns might be missing.

Do you want to submit a PR?

CyborgMaster commented 9 years ago

Sure! I will submit a PR as soon as I get our full test suite passing, so probably sometime this evening.

byroot commented 9 years ago

Then don't worry it will be faster if I do :)

CyborgMaster commented 9 years ago

Oh ok. Thanks! Sorry. I just wanted to make sure it was actually the solution to my problem before I pushed it your way. At this point, I'm 90% sure it is, so go for it.

On Thu, Feb 5, 2015 at 3:19 PM, Jean Boussier notifications@github.com wrote:

Then don't worry it will be faster if I do :)

— Reply to this email directly or view it on GitHub https://github.com/byroot/activerecord-typedstore/issues/29#issuecomment-73142211 .

byroot commented 9 years ago

I just released a 0.5.2. Thanks a lot for your report.

CyborgMaster commented 9 years ago

Well awesome. You're super speedy! Thanks!