-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
[Feature] Customizeable current_user #938
Comments
I don't understand completely what you propose. Could you please explain it better? Do you want to be able to customize controller? If that is the case, you can simply override it in your application controller... |
At this point I'm not sure how or where the helper
I would like to be able to modify this line so I can optimize my application. For example, what I want right now is :
This would optimize how many SQL statements I use to generate my views. My needs would be different in another application, but being able to customize the helper |
It is a method in the controller, you can override it and call super. BUT, you don't need to do eager loading here. Eager loading would be just important if you had a collection of users. The nested eager loading can be done when bringing the flags. You can give as default parameter in the association. |
Ah okay, I was starting to think it wouldn't be that difficult. Thanks for confirming that I can override it. I need eager loading on current_user.votes because I'm essentially calling this 30 times (for different post objects)
Without eager loading, I get 30 of these for each page request:
Where the only difference is the post_id. I could rearrange my Rails statement so that I could eager load Post.votes, but for my particular application I feel like that wouldn't scale well. I'm not sure what you mean by "bringing the flags"--could you elaborate on that? |
Ah, I didn't know find_by_post_id would look into the cache. Maybe you can do something like this in your application controller:
I am not sure if user can be nil in this case... you can try it out. :) |
I'm still new to eager loading, but I believe it prevents database lookups for all data you include, thus if we have
then the following code would still just be 2 sql statements (something like
However, as something like I'm on rails 1.8.7, so I can't use tap, but I'll just use debugger to try to accomplish the same. Thanks for that code snippet! l Am I correct that |
After some more playing around, I discovered that you were right that ActiveRecord:Base.find does not use eager-loaded values. However, current_user does not eager-load any associations. I tested this using the
in my ~/.irbrc file. Then, setting
The second current_user.posts doesn't generate an SQL query since Rails caches that. I don't think I can modify current_user to eager load associations, though, since it appears to be a copy of the model object, provided by Warden. I'd be willing to edit this conversation (for length) and make a writeup in the wiki, if you'd like. Since I'm still pretty new to Rails, I'm not sure how much of what I've discovered is obvious to most users. |
Thanks for pinging Back. It is indeed obvious for experienced users, but again, there is no need to eager load on users. You would have to eager load if you had an array of users instead. |
No problem. When you say that there is no need to eager load a user, are you referring to the user associations? When I have |
In your case, if you want to bring all posts, you just do @user.posts.all. However, when we refer to eager loading in Rails, we usually say that instead of doing this:
You should do this:
So it does two queries instead of N + 1. |
That part I understand and I agree it's entirely doable when working within the User model. I ran into issues when I wanted to do something in the view for Posts#index. Specifically, I wanted to display different vote links for each post based on whether I suppose I could create an instance variable |
Came across this issue today myself on wanting to specifically eager load on current_user. In my case, a user has_many decks (study deck - flash card app) and when I do I believe I understand what josevalim is saying for eager loading on other models in other cases, but there are specific times where one would want to eager-load associations on current_user (and Rails does not eager-load them automatically, at least not by default that I can see) |
current_user.decks will always eager load. Unless you are calling a method that makes it goes in batch. You can call current_user.decks.all to be sure. What would not be eager loaded is if you have something nested on decks, like cards. So you would have to do: current_user.decks.all(:include => :cards). |
Apologies, you are absolutely right about that, |
Yes, joeellis, that is one possible solution for me. I've extracted a lot of logic out of my views into helper methods, so I'm concerned that I'd run into performance issues. I wanted to avoid making ~30 calls of Even if it is expensive, there are still workarounds to this. I could make my helper method less generic, which I'd prefer not to do but I will if it will give the best performance. My initial impression was that |
Not sure without seeing your code, but it sounds like if you're worried about scalability, all you'd need to do is just cache it once and then do your ~30 calls, and just make sure to update the cache when the user makes changes to its posts On Thursday, March 24, 2011 at 3:20 PM, eric-hu wrote:
|
Scalability is indeed the concern and your solution sounds like it'll work, but I think that's starting to go out of the scope of this issue. Sorry for all the notifications, Jose, and thanks for your patience! |
It would be useful to have a generator function similar to the views generator so that the current_user loading could be customized. In my current situation, I'd like to be able to add some eager loading and nested eager loading to current_user, so that I could do something like
without having to query the database
The text was updated successfully, but these errors were encountered: