[Feature][ActiveRecord] Finding Orphans

Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

[Feature][ActiveRecord] Finding Orphans

Tom Rossi
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

John Pollard
I definitely find myself looking for orphans to clean up my database. What about adding a method scope as "missing_#{relationship_name}"

Book.missing_author #without a parent

Author.missing_books #without children


On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

Nathaniel Suchy
In reply to this post by Tom Rossi
That functionality sounds really useful. The code you showed you use above to find orphans seems a bit more complex than an easy to use ActiveRecord method. I look forward to seeing what other people say about this idea :)

On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

Joshua Stowers
In reply to this post by John Pollard
This reminds me of another recent topic: https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/wAkJbUb3mio
They're discussing the idea of adding a presence scope with `Person.where.present(:name)`
If this method also checks for the presence of associated records like `Person.where.present(:parent)` then it would make sense to also have a method that does the opposite (like you're proposing).
Or perhaps it would simply make more sense to instead use `Person.where.not.present(:parent)`.


On Monday, December 10, 2018 at 7:55:51 PM UTC-6, John Pollard wrote:
I definitely find myself looking for orphans to clean up my database. What about adding a method scope as "missing_#{relationship_name}"

Book.missing_author #without a parent

Author.missing_books #without children


On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

DHH-2
I even like Person.where.missing(:parent). The key distinguisher is to use modifiers on #where, just like we have with #not, rather than hang these off the root scope.

On Tuesday, December 11, 2018 at 3:38:36 PM UTC-8, Joshua Stowers wrote:
This reminds me of another recent topic: <a href="https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/wAkJbUb3mio" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/wAkJbUb3mio&#39;;return true;" onclick="this.href=&#39;https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/wAkJbUb3mio&#39;;return true;">https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/wAkJbUb3mio
They're discussing the idea of adding a presence scope with `Person.where.present(:name)`
If this method also checks for the presence of associated records like `Person.where.present(:parent)` then it would make sense to also have a method that does the opposite (like you're proposing).
Or perhaps it would simply make more sense to instead use `Person.where.not.present(:parent)`.


On Monday, December 10, 2018 at 7:55:51 PM UTC-6, John Pollard wrote:
I definitely find myself looking for orphans to clean up my database. What about adding a method scope as "missing_#{relationship_name}"

Book.missing_author #without a parent

Author.missing_books #without children


On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

Deepak Mahakale
In reply to this post by Tom Rossi
This sounds interesting.  

On Wednesday, 28 November 2018 04:11:53 UTC+5:30, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

Tom Rossi
In reply to this post by DHH-2
I'd like to give this a shot! I am thinking this will be added to the activerecord/lib/active_record/relation/query_methods.rb to the WhereChain class. The missing method will accept an individual or array of relationships. The resulting scope will have a left join on each relationship as well as a where clause for each table with the id set nil:

# New WhereChain method:
Post.where.missing(:author)

# Equivalent:
Post.left_joins(:author).where(authors: { id: nil })

# Resulting sQL
# SELECT "posts".* FROM "posts" LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id" WHERE "authors"."id" IS NULL


On Wed, Dec 12, 2018 at 4:57 PM DHH <[hidden email]> wrote:
I even like Person.where.missing(:parent). The key distinguisher is to use modifiers on #where, just like we have with #not, rather than hang these off the root scope.

On Tuesday, December 11, 2018 at 3:38:36 PM UTC-8, Joshua Stowers wrote:
They're discussing the idea of adding a presence scope with `Person.where.present(:name)`
If this method also checks for the presence of associated records like `Person.where.present(:parent)` then it would make sense to also have a method that does the opposite (like you're proposing).
Or perhaps it would simply make more sense to instead use `Person.where.not.present(:parent)`.


On Monday, December 10, 2018 at 7:55:51 PM UTC-6, John Pollard wrote:
I definitely find myself looking for orphans to clean up my database. What about adding a method scope as "missing_#{relationship_name}"

Book.missing_author #without a parent

Author.missing_books #without children


On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: [Feature][ActiveRecord] Finding Orphans

Kasper Timm Hansen
Once you’re getting down to specific code choices, it’s better in a PR. So feel free to submit one 👍

You also don’t have to get everything right up front as long as you help guide reviewers. For instance, it's fine to submit a piecemeal PR, just describe what you intend to defer for your second/third pass (e.g. tests, documentation, CHANGELOG entry).

Den 13. dec. 2018 kl. 19.45 skrev Tom Rossi <[hidden email]>:

I'd like to give this a shot! I am thinking this will be added to the activerecord/lib/active_record/relation/query_methods.rb to the WhereChain class. The missing method will accept an individual or array of relationships. The resulting scope will have a left join on each relationship as well as a where clause for each table with the id set nil:

# New WhereChain method:
Post.where.missing(:author)

# Equivalent:
Post.left_joins(:author).where(authors: { id: nil })

# Resulting sQL
# SELECT "posts".* FROM "posts" LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id" WHERE "authors"."id" IS NULL


On Wed, Dec 12, 2018 at 4:57 PM DHH <[hidden email]> wrote:
I even like Person.where.missing(:parent). The key distinguisher is to use modifiers on #where, just like we have with #not, rather than hang these off the root scope.

On Tuesday, December 11, 2018 at 3:38:36 PM UTC-8, Joshua Stowers wrote:
They're discussing the idea of adding a presence scope with `Person.where.present(:name)`
If this method also checks for the presence of associated records like `Person.where.present(:parent)` then it would make sense to also have a method that does the opposite (like you're proposing).
Or perhaps it would simply make more sense to instead use `Person.where.not.present(:parent)`.


On Monday, December 10, 2018 at 7:55:51 PM UTC-6, John Pollard wrote:
I definitely find myself looking for orphans to clean up my database. What about adding a method scope as "missing_#{relationship_name}"

Book.missing_author #without a parent

Author.missing_books #without children


On Tuesday, November 27, 2018 at 5:41:53 PM UTC-5, Tom Rossi wrote:
Its common for me to look for orphans and I typically do it like this:

Child.eager_load(:parent).where(parents: { id: nil })

I would like to propose putting together a PR for something like this:

Child.missing(:parent)
Parent.missing(:children)

Please let me know if others are interested in the functionality and I would love to give it a shot!

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
Kasper

--
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 [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.