Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 132329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:14:43+00:00 2026-05-11T06:14:43+00:00

I have the following ActiveRecord classes: class User < ActiveRecord::Base cattr_accessor :current_user has_many :batch_records

  • 0

I have the following ActiveRecord classes:

class User < ActiveRecord::Base   cattr_accessor :current_user   has_many :batch_records end  class BatchRecord < ActiveRecord::Base   belongs_to :user    named_scope :current_user, lambda {     { :conditions => { :user_id => User.current_user && User.current_user.id } }   } end 

and I’m trying to test the named_scope :current_user using Shoulda but the following does not work.

class BatchRecordTest < ActiveSupport::TestCase   setup do     User.current_user = Factory(:user)   end    should_have_named_scope :current_user,                           :conditions => { :assigned_to_id => User.current_user } end 

The reason it doesn’t work is because the call to User.current_user in the should_have_named_scope method is being evaluated when the class is being defined and I’m change the value of current_user afterwards in the setup block when running the test.

Here is what I did come up with to test this named_scope:

class BatchRecordTest < ActiveSupport::TestCase   context 'with User.current_user set' do     setup do       mock_user = flexmock('user', :id => 1)       flexmock(User).should_receive(:current_user).and_return(mock_user)     end      should_have_named_scope :current_user,                             :conditions => { :assigned_to_id => 1 }   end end 

So how would you test this using Shoulda?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:14:44+00:00Added an answer on May 11, 2026 at 6:14 am

    I think you are going about this the wrong way. Firstly, why do you need to use a named scope? Wont this just do?

    class BatchRecord < ActiveRecord::Base   belongs_to :user    def current_user     self.user.class.current_user   end end 

    In which case it would be trivial to test. BUT! WTF are you defining current_user as a class attribute? Now that Rails 2.2 is ‘threadsafe’ what would happen if you were running your app in two seperate threads? One user would login, setting the current_user for ALL User instances. Now another user with admin privileges logs in and current_user is switched to their instance. When the first user goes to the next page he/she will have access to the other persons account with their admin privileges! Shock! Horror!

    What I reccomend doing in this case is to either making a new controller method current_user which returns the current user’s User instance. You can also go one step further and create a wrapper model like:

    class CurrentUser    attr_reader :user, :session    def initialize(user, session)     @user, @session = user, session   end    def authenticated?     ...   end    def method_missing(*args)     user.send(*args) if authenticated?   end  end 

    Oh, and by the way, now I look at your question again perhaps one of the reasons it isn’t working is that the line User.current_user && User.current_user.id will return a boolean, rather than the Integer you want it to. EDIT I’m an idiot.

    Named scope is really the absolutely wrong way of doing this. Named scope is meant to return collections, rather than individual records (which is another reason this fails). It is also making an unnecessary call the the DB resulting in a query that you don’t need.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The Allow and Disallow lines in robots.txt say, "allow (or… May 12, 2026 at 4:17 pm
  • Editorial Team
    Editorial Team added an answer Yes, you can execute plain SQL queries with Hibernate. No,… May 12, 2026 at 4:17 pm
  • Editorial Team
    Editorial Team added an answer The # style works for me: {@link Planet#EARTH} The key… May 12, 2026 at 4:17 pm

Related Questions

I have a Menu class that has a IQueryable property called WebPages. In the
I'm attempting to create a simple web application for some personal development, but I've
I have the following 3 rails classes, which are all stored in one table,
I have a rails application that patches ActiveRecord with a hand-coded validator. The patch

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.