Couldn't find User with ID=5
I've had this problem all along, including on users with id 1, 2, 3,
etc. If I kick the app on with script/console, I can do a User.find
(5), and get an actual User back without problems. The problem
persists until I have actually activated the user:
User.find(5).activate
And then everything is fine again. I have no clue why it can't find
the user with ID 5, which clearly exists. The stack trace is below.
Any idea what is going on? I'm not new to Rails (though it has
admittedly been a little while), but this error seems a bit slippery.
Clearly User.find() works, as there would be a whole world of hurt in
most likely every page of the application (not to mention it works
fine from Console), but for some reason on the site_index page (and
that page alone, so far as I can tell), it buggers out.
Thoughts?
regards,
Matt
>>
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1591:in `find_one'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:1574:in `find_from_ids'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:616:in `find_without_pagination'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/plugins/
paginating_find/lib/paginating_find.rb:104:in `find'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/models/
user.rb:93:in `find'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/models/
user.rb:166:in `find_by_activity'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/models/
user.rb:166:in `map'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/models/
user.rb:166:in `find_by_activity'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
named_scope.rb:186:in `send'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
named_scope.rb:186:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
base.rb:2148:in `with_scope'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
named_scope.rb:179:in `method_missing'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/
controllers/base_controller.rb:128:in `get_additional_homepage_data'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/
controllers/base_controller.rb:41:in `site_index'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:135:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:135:in `custom'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:179:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:179:in `respond'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:173:in `each'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:173:in `respond'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/mime_responds.rb:107:in `respond_to'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/app/
controllers/base_controller.rb:40:in `site_index'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:1322:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:1322:in `perform_action_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:617:in `call_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:638:in `run_before_filters'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/lib/
localized_application.rb:25:in `set_locale'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/
active_support/callbacks.rb:178:in `send'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/
active_support/callbacks.rb:178:in `evaluate_method'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:186:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:635:in `run_before_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:615:in `call_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:610:in `perform_action_without_benchmark'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/
active_support/core_ext/benchmark.rb:17:in `ms'
/usr/local/lib/ruby/1.8/benchmark.rb:308:in `realtime'
/usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/
active_support/core_ext/benchmark.rb:17:in `ms'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/benchmarking.rb:68:in
`perform_action_without_rescue'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/rescue.rb:160:in `perform_action_without_flash'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/flash.rb:141:in `perform_action'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:523:in `send'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:523:in `process_without_filters'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/filters.rb:606:in `sass_old_process'
/usr/local/lib/ruby/gems/1.8/gems/haml-2.0.5/lib/sass/plugin/rails.rb:
19:in `process'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:391:in `process'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/base.rb:386:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/routing/route_set.rb:433:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:88:in `dispatch'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:111:in `_call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:82:in `initialize'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/lib/
flash_session_cookie_middleware.rb:14:in `call'
/home/mlinnell/web/Nebulae/vendor/plugins/community_engine/lib/
flash_session_cookie_middleware.rb:14:in `call'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:29:in `call'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/query_cache.rb:34:in `cache'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:9:in `cache'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
query_cache.rb:28:in `call'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/
connection_adapters/abstract/connection_pool.rb:361:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/head.rb:9:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/methodoverride.rb:24:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/params_parser.rb:15:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/rewindable_input.rb:25:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/session/cookie_store.rb:93:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/reloader.rb:9:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/failsafe.rb:11:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/lock.rb:11:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/lock.rb:11:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/dispatcher.rb:106:in `call'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb:
31:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/urlmap.rb:46:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/urlmap.rb:40:in `each'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/urlmap.rb:40:in `call'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/
log_tailer.rb:17:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/content_length.rb:13:in `call'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/handler/webrick.rb:46:in
`service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/usr/local/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/usr/local/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/usr/local/lib/ruby/1.8/webrick/server.rb:95:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `each'
/usr/local/lib/ruby/1.8/webrick/server.rb:92:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:23:in `start'
/usr/local/lib/ruby/1.8/webrick/server.rb:82:in `start'
/usr/local/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/
action_controller/vendor/rack-1.0/rack/handler/webrick.rb:13:in `run'
/usr/local/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:
111
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
/usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3
In the Rails log, I see this as the last SQL statement prior to
erroring out:
User Load (0.2ms) SELECT * FROM "users" WHERE ("users"."id" = 5)
AND (users.activated_at IS NOT NULL)
It appears this is coming from line 166 of user.rb, which is part of
User.find_by_activity. Specifically, line 166 is grabbing all the
users that were found by the Activity lookup, and does a find on User
based on the foreign key user_id to get the actual users
activities.map{|a| find(a.user_id) }
It is not clear to me how the WHERE clause above gets added to what
seems to be a normal find(id) (something to do with a find with a
scope?), but clearly this find is returning nil because there is no
user 5 that is also active.
> activities.map{|a| find(a.user_id) }
I ran into this exact same problem recently. You are absolutely
correct, in the line above find is being called with a scope. In this
case from a call in base_controller (scope: active).
User.active.find_by_activity({:limit => 5, :require_avatar => false})
The call to Activity.since doesn't have this scope and can return
unactivated users if they created an account but didn't activate it
yet. I found the following change already in edge which adds another
condition to ensure the activity only returns active users.
http://github.com/bborn/communityengine/commit/21836ef1e9df5051a590f7f1f055f3ef5cd13776#L1L160
- :conditions => "#{options[:require_avatar] ? ' users.avatar_id
IS NOT NULL' : nil}",
+ :conditions => "#{options[:require_avatar] ? ' users.avatar_id
IS NOT NULL AND ' : ''} users.activated_at IS NOT NULL",
The additional scoping could probably be removed from the function
call as well, but for now I have made the change above and it works.
Justin
-Matt
On Dec 21, 5:01 pm, Justin G <jtgei...@gmail.com> wrote:
> Matt,
>
> > activities.map{|a| find(a.user_id) }
>
> I ran into this exact same problem recently. You are absolutely
> correct, in the line above find is being called with a scope. In this
> case from a call in base_controller (scope: active).
> User.active.find_by_activity({:limit => 5, :require_avatar => false})
>
> The call to Activity.since doesn't have this scope and can return
> unactivated users if they created an account but didn't activate it
> yet. I found the following change already in edge which adds another
> condition to ensure the activity only returns active users.
>
> http://github.com/bborn/communityengine/commit/21836ef1e9df5051a590f7...
I'm pretty sure you could do something like "git cherry-pick <commit>"
to pull just that patch in. You might have to do a "git fetch" first
to make sure you have the latest data from the server.
The only reason I didn't try this was that I didn't need the other
changes in the commit to fix the problem I was seeing. In the end I
pulled in everything from edge (I think to get a jump on the switch to
authlogic). Probably not the best idea since I intend to take this
live soon, but I haven't seen any problems yet. (By the way, git
didn't have any issue automatically merging my change with the
identical change on edge.)
Bruno,
While I'm on the subject, I have a few commits that I would like to
send to you. Unfortunately they are currently mixed in on a branch
that I have been doing heavy customization to. What do you think is
the best way to handle this? I'm thinking that once I identify the
commits that should go upstream, I can cherry-pick them into a new
branch and then send you a pull request.
Justin
While I'm on the subject, I have a few commits that I would like to
send to you. Unfortunately they are currently mixed in on a branch
that I have been doing heavy customization to. What do you think is
the best way to handle this? I'm thinking that once I identify the
commits that should go upstream, I can cherry-pick them into a new
branch and then send you a pull request.
Justin
--
You received this message because you are subscribed to the Google Groups "CommunityEngine" group.
To post to this group, send email to communi...@googlegroups.com.
To unsubscribe from this group, send email to communityengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/communityengine?hl=en.
> For the record; you should rarely be
> touching the CE plugin code (unless you're doing a bugfix or feature
> addition that is intended to go back into core). Your local application
> customizations should be done in your app directory, not directly to CE.
I have read this before but at the time (about a year ago) when I
first started hacking on CE I didn't follow your advise. The
advantage I see with a separate branch is that I have to manually fix
any merge conflicts. This lets me know when we modify the same places
in the app. Now if I was a good Rails citizen and wrote/ran test
scripts frequently it might not be as big of a problem, but at the
time I felt more comfortable jumping into (my first) git workflow
compared to overriding entire methods/views in my own app folder.
A lot of this has to do with the types of changes I wanted to make
initially. For example, one of the first things I did was change from
a location based on country/state/city to one based on region/county/
neighborhood (my site is local and I needed more granularity). This
meant I was modifying a lot of files to make 1 or 2 line changes. I
was worried if I copied all of these files to my own app that I would
be stuck at a later time synchronizing them by hand to bring in any
new goodness from core (master). Instead I let git handle this
automatically and I only have to deal with the conflicts.
I do agree that we should strive to make CE as modular as possible. I
am working on some other features which I have broken off into a
separate plugin. Here it made sense because I wasn't overriding
functionality, only adding. Most importantly it gave me a convenient
way to manage my migrations separately.
Unfortunately, I must admit, during my last round of development my
methods resulted in some commits that belong upstream getting stuck in
a heavily a customized branch. I would like to make some of these
changes more accessible to others. I plan to cherry-pick commits with
upstream potential into a separate branch for your review. Going
forward this is the workflow I will probably follow. I am open to any
suggestions for improvement:
fixes branch - for things that belong upstream or may be of general
use to the community.
project branch - for all the customizations specific to my project.
Probably not useful to others.
new plugin - for new functionality not directly coupled with existing
CE core functionality.
Thanks,
Justin