Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActiveRecord querying not working in > 0.1.14 #41

Closed
goalaleo opened this issue Jun 6, 2017 · 12 comments
Closed

ActiveRecord querying not working in > 0.1.14 #41

goalaleo opened this issue Jun 6, 2017 · 12 comments

Comments

@goalaleo
Copy link

goalaleo commented Jun 6, 2017

Behaviour in 0.1.14

[4] pry(main)> User.first
  User Load (1.0ms)  SELECT  "users".* FROM "users" ORDER BY "users"."created_at" ASC LIMIT $1  [["LIMIT", 1]]
=> #<User id: "8ec92fd5-5926-4657-9b32-8cf8382de795", email: "some@example.com", created_at: "2017-05-16 10:19:37", updated_at: "2017-05-16 10:19:37", first_name: nil, last_name: nil, guest: false>

Behaviour in 0.1.15 and 0.1.16

[4] pry(main)> User.first
NoMethodError: undefined method `[]' for nil:NilClass
from /Users/<user>/.rvm/gems/ruby-2.4.1/gems/activerecord-5.1.0/lib/active_record/relation/delegation.rb:5:in `relation_delegate_class'

Coming from this line: https://github.com/rails/rails/blob/v5.1.0/activerecord/lib/active_record/relation/delegation.rb#L5

I wanted to upgrade the gem, because I was getting a lot of these deprecation warnings:

DEPRECATION WARNING: The behavior of `changed_attributes` inside of after callbacks will be changing in the next version of Rails. The new return value will reflect the behavior of calling the method after `save` returned (e.g. the opposite of what it returns now). To maintain the current behavior, use `saved_changes.transform_values(&:first)` instead. (called from create_guest_user at /Users/<user>/Desktop/<project>/app/models/user.rb:28)

line causing them is just user.save!, and a colleague thought that they might be coming from mobility. Seems like this is related rails/rails#28640

@shioyama
Copy link
Owner

shioyama commented Jun 6, 2017

Thanks! Yes the deprecation issues in Rails 5.1 (I guess that's what you're using?) are a known issue. It's top on my priority issues to fix. I know basically what's going wrong but I need a bit of time to fix it.

That said, I haven't seen any actual exceptions, and certainly specs would be failing if something as simple as first was breaking like that in general.

Can you show me what's in your User model? Do you have default_scope { i18n } or something like that? Mobility is pretty hands-off so unless you have something like that, it shouldn't be interfering with standard AR query methods like that.

@shioyama
Copy link
Owner

shioyama commented Jun 6, 2017

Never mind, I see from your console snippet that you are using AR 5.1. So the deprecation errors are a known issue in that version.

Again, though, the exception is very strange...

@shioyama
Copy link
Owner

shioyama commented Jun 6, 2017

What backend are you using btw? Probably not related but just in case.

@shioyama shioyama added the bug label Jun 6, 2017
@goalaleo
Copy link
Author

goalaleo commented Jun 7, 2017

Can you show me what's in your User model? Do you have default_scope { i18n } or something like that?

Ah, yes I have a default_scope defined because we're using uuid's, and
none of the Rails finder methods using find_nth worked as expected. Here's the default_scope

# application_record.rb
def self.default_scope
  if uses_uuid?
    order(created_at: :asc)
  else
    order(id: :asc)
  end
end

def self.uses_uuid?
  columns_hash["id"].type == :uuid
end

Do you have an Idea why this breaks it? Other query methods aren't working either e.g. find_by and where are throwing the same exception. I pasted the contents of my Gemfile and Gemfile.lock here: https://pastebin.com/T13zRR8v

@shioyama
Copy link
Owner

I will check this out probably early next week, but seems to be limited to your use of default_scope, there may be some conflict with the uniqueness validation change which was introduced in 0.1.15 (although I don't know why validation would be triggered when querying...)

@ArturLyapin
Copy link

I have the same problem.
Isolated reproduction here: https://gist.github.com/ArturLyapin/deee21d1f557c0b87bdc682ac8292e7a

@shioyama
Copy link
Owner

@ArturLyapin Thanks, that gist helps a lot. I'll look into it now.

@shioyama
Copy link
Owner

@goalaleo @ArturLyapin I've released 0.1.17 which should fix the STI issues.

Deprecation warnings in Rails 5.1 are not fixed yet, I know they are very noisy so I will try to fix them in the next week or so, but code should work in 5.1, just ignore the warnings for now.

@shioyama
Copy link
Owner

@goalaleo The deprecation warnings issue has been fixed on the master branch. I'll release a new version in the next day or so but if you want to get rid of them, just use master:

gem 'mobility', github: 'shioyama/mobility'

And bundle update mobility.

@goalaleo
Copy link
Author

@shioyama great news - thanks for the info! I'll wait until the official release.

@shioyama
Copy link
Owner

0.1.18 has been released, give it a try 😄

@goalaleo
Copy link
Author

@shioyama seems to work for me - no more deprecation warnings. Cheers 👏 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants