-
-
Notifications
You must be signed in to change notification settings - Fork 11
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
General feedback #3
Comments
Looks like a good approach! Did you try showing this to anyone from the Rails team? I don't think there's a huge chance they will adopt this but it's worth hearing their feedback. |
@yoelblum No i didn't show it to the Rails team. In 2015, someone did a PR for similar feature (not as fleshed out in the details) and it got refused just based on somone being against the feature. I don't know if their mood changed related to this. The guy ended up making a gem with it, and we didn't hear much about it after that sadly. |
I'm doing a lot of filtering logic in my apps, and I'd often run into bugs when combining several scopes which do joins. I've just found this gem yesterday, and it's really helpful. Is this likely to survive rails upgrades? I'm a bit wary with gems that extend activerecord, because they tend to have a hard time going through major rails upgrades. |
Glad you find it helpful! This is essentially the same codebase that deals with ActiveRecord 4.1 all the way to ActiveRecord 5.2. I'm not doing some deep hacks of changing how ActiveRecord behaves. All that the gem does is generate a single The CI build on Travis even has a job to run against ActiveRecord's master branch, so I can prepare in advance if something starts breaking from the ongoing development. Thanks for the feedback! |
That sounds good! Do you have any data on how this performs compared to joins? Especially with > 1 million rows? Is it faster? Is it slower? Is it comparable? If you have any data on this, it might make a good addition to the README. |
SQL-wise, it should normally be either comparable or faster to use However, when dealing with has_one, it is probable that this gem will be slower (can't really say by how much). This is because this gem will correctly filter based on only the "first" record of the association, but figuring out which record is that "first" is more work (Need to apply ordering). The |
Thanks for this gem! I've been writing things like this for years:
Having a gem to do it for me would be so much better! Since you asked for general feedback, here are two things I notice: Imagine you have 3 tables:
The 2 improvements are:
If these sound like improvements to you, I'd be willing to write up separate issues for them and look at sending you PRs. What do you think? |
@pjungwir Hey, thank you for the feedback! Glad you find the gem useful.
"Would be" implies that you don't use the gem? Is the missing handling of For your issue with the placeholders Using
And I don't see any benefit to using |
I'm not using it yet. I just discovered it. I don't think either of the issues are enough to stop me from using it. Thanks for giving your thoughts about both those suggestions! To me, using a join feels like the more important of the two, although I don't have any good reasons to back that up. I think at least for Postgres you don't need to add table aliases. For example you can do this (not that it means anything):
Personally I think it is more mentally complex to have two ways a Anyway, maybe I'll look into using parameters at least. |
I see what you mean about not needing alias. But that only works if the only "reused" name is the last one. Imagine a weird relation that goes Also, doing a What do you refer to when you say "usually with a When data from another table must be returned, then These are different operations, it makes sense that they use different operators. Here are 3 relations which do the same thing. I like that they generate the same SQL query. User.where_assoc_exists(:roles, name: 'foo')
User.where_assoc_exists(:user_roles) {|ur| ur.where_assoc_exists(:role, name: 'foo') }
User.where_assoc_exists([:user_roles, :role], name: 'foo') I like this discussion, if you'd like to continue it, I would prefer if you could make a separate Gitub issue for it, it is out of the scope of general feedback. You can quote this post and reply to it there. Thank you for your interest! |
Is there a way to check if the association was already added? To avoid double-ups. For instance: user = User.where_assoc_exists(:roles)
# passed to another method that also adds the assoc
user = user.where_assoc_exists(:roles) Would produce a query that joins on roles twice. I would like to check if it's already added, so I can avoid adding it again. |
Good question. While not as clean as normal usage. I guess you could use Having that SQL string, you could then check in the relation's Good luck |
Thanks for the gem, I am curious if there is a way to use a block with a nested Using the example here activerecord_where_assoc/lib/active_record_where_assoc/relation_returning_methods.rb Line 343 in 04d533c
I seem to be able to get it to work with something like
Can't figure out if there is a way to access both levels of nesting within a block. If I do
|
Hello @jonny5, sadly, there is no way to do this at the moment. I didn't think of that use-case when developping this gem. For the As you found out, SQL's nesting can allow you to it when using strings, so that's a pretty good work-around. Since |
btw, some followup on my question above: I wasn't able to use |
If you have any feedback (good or bad) about the gem that you don't feel is worthy of an issue, please post a comment here.
I'd be really interested in why you decided not to use this gem.
The text was updated successfully, but these errors were encountered: