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

Problem with joins #202

Open
dawidof opened this issue Feb 22, 2021 · 7 comments
Open

Problem with joins #202

dawidof opened this issue Feb 22, 2021 · 7 comments

Comments

@dawidof
Copy link

dawidof commented Feb 22, 2021

Does not create proper sql when paranoid set on joined table
Screenshot 2021-02-22 at 18 51 02

@mvz
Copy link
Contributor

mvz commented Feb 23, 2021

Hi @dawidof can you please provide a way to reproduce this problem? At first glance, the SQL seems fine, but without the code it's hard to tell.

@dawidof
Copy link
Author

dawidof commented Feb 23, 2021

Thanks @mvz, yeah you are right, it seems like error in my code.

But I've got another problem with using with_deleted on joined table

Screenshot 2021-02-23 at 11 39 44

@mvz
Copy link
Contributor

mvz commented Feb 23, 2021

@dawidof I'll also need some code to reproduce this. I can't tell from your screen shot what you think is wrong.

@dawidof
Copy link
Author

dawidof commented Feb 23, 2021

@mvz I've not tested, but I think this code would be enough

class Provider  < ApplicationRecord
  has_many :licences, dependent: :destroy
end

class Licence < ApplicationRecord
  acts_as_paranoid
  belongs_to :provider
end

@mvz
Copy link
Contributor

mvz commented Feb 23, 2021

Can you please also say

  • what you're trying to do
  • what happens that you didn't expect
  • what you expected to happen instead

@dawidof
Copy link
Author

dawidof commented Feb 23, 2021

E.g. I'm trying to get providers with active and not dependent on deleted_at column licences.

on Provider.joins(:licences).with_deleted.active.count
it throws an error NoMethodError (undefined method 'with_deleted' for #<Provider::ActiveRecord_Relation:0x00007fc7b3a37230>)

I am expecting to get collection with providers, starting AR query request with Provider model

@mvz
Copy link
Contributor

mvz commented Apr 25, 2021

If you start from Provider you only get scopes that are defined in Provider. Since Provider is not paranoid, it does not provide the with_deleted scope. Even after .joins(:licenses), you still will not get scopes defined in License.

There's an open issue about the ability to specify that joined relations should include deleted records here: #110.

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

No branches or pull requests

2 participants