[Feature request] has_many option to generate additionnal has_many for each given scope.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[Feature request] has_many option to generate additionnal has_many for each given scope.

Julien Grillot
Hi there!

I am writing a new option for `has_many` associations. It generates additionnal has_many for each given association scope.

    # Before
    has_many :posts
    has_many :published_posts, -> { published }, class_name: "Post"

    # After
    has_many :posts, new_option: [:published]

Why
In my rails projects, I really often write a lot of `has_many` variations to be able to use fine-grained preload. Eg: `Author.all.preload(:published_post).each do [...]`.

It would be handful to have syntaxic sugar to shorten all theses `has_many`

What I've done
https://github.com/Bahanix/rails/commit/15ec54d175a8936e8f1017a77a160760c8fb92a2
I am very unsure about my `compose` option name (I am not a native english speaker).
Does the feature seems useful to you / relevant for an ActiveRecord PR?

Thank you for reading :)
Julien Grillot

--
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
|  
Report Content as Inappropriate

Re: [Feature request] has_many option to generate additionnal has_many for each given scope.

Julien Grillot
Here is a (unpublished) post I wrote about this feature:

https://medium.com/octoly-tech/activerecord-has-many-compose-option-4f2cbdb524a3

Perhaps a better name for this option may be `preloadable_scopes`—it does not cover all things one can do with this feature, but it is a quite self-explained option name since preloading theses generated has_many would be (I think) its main usage.

--
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
|  
Report Content as Inappropriate

Re: [Feature request] has_many option to generate additionnal has_many for each given scope.

Julien Grillot
Here is the PR: https://github.com/rails/rails/pull/28626

Does somebody have a thought on it?

--
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
|  
Report Content as Inappropriate

Re: [Feature request] has_many option to generate additionnal has_many for each given scope.

Joshua Flanagan
The "before" version really doesn't seem that painful. Compared with the cost to the rails team to maintain the syntax, and the cost in understandability to other developers that happen upon your code. DSLs are only helpful when they are well understood by everyone that has to read them - something like this would be very rarely used, so every dev that encounters it in the wild will have to look up what "compose" means, (or try to figure out where published_posts is defined if they dont know the trick is to look at the posts has_many definition) to try and figure out the magic.

I think you're better off being more explicit in these cases.

On Sunday, April 9, 2017 at 4:36:31 PM UTC-5, Julien Grillot wrote:
Here is the PR: <a href="https://github.com/rails/rails/pull/28626" target="_blank" rel="nofollow" onmousedown="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F28626\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHQs8rCuX7zhampwjxes2bH3ff42w&#39;;return true;" onclick="this.href=&#39;https://www.google.com/url?q\x3dhttps%3A%2F%2Fgithub.com%2Frails%2Frails%2Fpull%2F28626\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNHQs8rCuX7zhampwjxes2bH3ff42w&#39;;return true;">https://github.com/rails/rails/pull/28626

Does somebody have a thought on it?

--
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
|  
Report Content as Inappropriate

Re: [Feature request] has_many option to generate additionnal has_many for each given scope.

Kevin Deisz
Probably a better first step would be to move this into a gem and then if it garners wide enough adaption making the case then.

On Mon, Apr 10, 2017 at 11:16 AM, Joshua Flanagan <[hidden email]> wrote:
The "before" version really doesn't seem that painful. Compared with the cost to the rails team to maintain the syntax, and the cost in understandability to other developers that happen upon your code. DSLs are only helpful when they are well understood by everyone that has to read them - something like this would be very rarely used, so every dev that encounters it in the wild will have to look up what "compose" means, (or try to figure out where published_posts is defined if they dont know the trick is to look at the posts has_many definition) to try and figure out the magic.

I think you're better off being more explicit in these cases.

On Sunday, April 9, 2017 at 4:36:31 PM UTC-5, Julien Grillot wrote:
Here is the PR: https://github.com/rails/rails/pull/28626

Does somebody have a thought on it?

--
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.



--
Kevin D. Deisz
Localytics Software Engineer

--
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.
Loading...