I've created a simple unit test that causes the error:
def test_inactive_user_activity_makes_error
q = users(:quentin)
q.track_activity(:updated_profile)
q.deactivate
assert q.activated_at.nil?
# Error occurs in the following line
users = User.active.find_by_activity({:limit => 5, :require_avatar =>
false})
assert ! users.include?(q)
end
This causes the ActiveRecord::RecordNotFound exception. Again, this shouldn't
have anything to do with controllers as I put this unit test into the
users_test.rb unit tests.
I've thought about modifying User.find_by_activity to only look for active
users as well, but for some reason the join SQL isn't working like I think it
should. I tried this:
def self.find_by_activity(options = {})
options.reverse_merge! :limit => 30, :require_avatar => true, :since =>
7.days.ago
activities = Activity.since(options[:since]).find(:all,
:select => 'activities.user_id, count(*) as count',
:group => 'activities.user_id',
:conditions => "#{options[:require_avatar] ? ' users.avatar_id IS NOT
NULL' : nil}",
:order => 'count DESC',
:joins => "LEFT JOIN users ON (users.id = activities.user_id and
users.activated_at is not null)",
:limit => options[:limit]
)
activities.map{|a| find(a.user_id) }
end
But it's still finding the activity of the inactive user. Perhaps someone with
better SQL fu could lead me in the right direction?
On May 11, 2010, at 6:25 PM, Patrick Minton wrote:
> The following query is doing some weird shit in my database:
>
> >> User.active.find_by_activity({:limit => 5, :require_avatar => false})
> ActiveRecord::RecordNotFound: Couldn't find User with ID=10
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1591:in
> `find_one'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1574:in
> `find_from_ids'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:in
> `find_without_pagination'
> from
> /apps/thecommunity/releases/20100509205223/vendor/plugins/community_engine/plugins/paginating_find/lib/paginating_find.rb:103:in
> `find'
> from
> /apps/thecommunity/releases/20100509205223/vendor/plugins/community_engine/app/models/user.rb:93:in
> `find'
> from
> /apps/thecommunity/releases/20100509205223/vendor/plugins/community_engine/app/models/user.rb:165:in
> `find_by_activity'
> from
> /apps/thecommunity/releases/20100509205223/vendor/plugins/community_engine/app/models/user.rb:165:in
> `map'
> from
> /apps/thecommunity/releases/20100509205223/vendor/plugins/community_engine/app/models/user.rb:165:in
> `find_by_activity'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:186:in
> `send'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:186:in
> `method_missing'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:2148:in
> `with_scope'
> from (__DELEGATION__):2:in `__send__'
> from (__DELEGATION__):2:in `with_scope'
> from
> /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/named_scope.rb:179:in
> `method_missing'
> from (irb):20
>
> There are two things that are weird about this:
>
> 1) There is, indeed, a user with that ID:
>
> >> u = User.find(10)
> => #<User id: 10, login: "devsample", email: "[email protected]",
> description: "", avatar_id: nil, crypted_password:
> "1ce245b87890a2323e7a67087415b1eb976bd9f3", salt:
> "bc615491c6881df432e99cf24b71712f520dd41a", created_at: "2010-02-24
> 22:27:30", updated_at: "2010-05-11 21:24:05", remember_token: nil,
> remember_token_expires_at: nil, stylesheet: nil, view_count: 3, vendor:
> false, activation_code: "c2f28946842f9822491e4f417d68978b0c63606c",
> activated_at: nil, state_id: nil, metro_area_id: nil, login_slug:
> "devsample", notify_comments: false, notify_friend_requests: true,
> notify_community_news: false, country_id: nil, featured_writer: false,
> last_login_at: "2010-02-24 22:31:08", zip: nil, birthday: "1980-01-01",
> gender: nil, profile_public: true, activities_count: 2, sb_posts_count: 0,
> sb_last_seen_at: "2010-05-11 21:22:00", role_id: 3, first_name: "Dev",
> middle_name: "", last_name: "Sample", company_id: nil, employer_id: nil,
> interests: "", community_hopes: "", community_promises: "", job_favorites:
> "", job_title: "">
>
> 2) That user isn't active so it shouldn't be within the User.active named
> scope and therefore should never be found by the find_by_activity query
> (should it?):
>
> >> u.active?
> => false
> >> u.activated_at
> => nil
>
> Anyone have any idea what's going on here? Looking at the code in user.rb
> it's kind of unclear to me how this function can limit the users found to
> those within the :active named scope because it just does a :
>
> def self.find_by_activity(options = {})
> options.reverse_merge! :limit => 30, :require_avatar => true, :since =>
> 7.days.ago
> #Activity.since.find(:all,:select => Activity.columns.map{|column|
> Activity.table_name + "." + column.name}.join(",")+', count(*) as
> count',:group => Activity.columns.map{|column| Activity.table_name + "." +
> column.name}.join(","),:order => 'count DESC',:joins => "LEFT JOIN users ON
> users.id = activities.user_id" )
> #Activity.since(7.days.ago).find(:all,:select => 'activities.user_id,
> count(*) as count',:group => 'activities.user_id',:order => 'count
> DESC',:joins => "LEFT JOIN users ON users.id = activities.user_id" )
> activities = Activity.since(options[:since]).find(:all,
> :select => 'activities.user_id, count(*) as count',
> :group => 'activities.user_id',
> :conditions => "#{options[:require_avatar] ? ' users.avatar_id IS NOT
> NULL' : nil}",
> :order => 'count DESC',
> :joins => "LEFT JOIN users ON users.id = activities.user_id",
> :limit => options[:limit]
> )
> activities.map{|a| find(a.user_id) }
> end
>
> And, in fact, if I do it without the named scope it works fine:
>
> >> users = User.find_by_activity({:limit => 5, :require_avatar => false}); nil
> => nil
> >> users.include?(u)
> => true
>
> So, it looks like the :active named scope doesn't actually change which users
> self.find_by_activity will find (because that isn't a generic ActiveRecord
> finder method, I assume), but *does* make ActiveRecord think that users that
> don't meet the conditions in that named scope aren't in the database. Anyone
> have any suggestions? This is making the site crash because this user
> updated their profile before deactivating and that profile update is one of
> the most recent activities.
>
>
>
>
--
You received this message because you are subscribed to the Google Groups
"CommunityEngine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/communityengine?hl=en.