Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think you are going about this the wrong way. Firstly, why do you need to use a named scope? Wont this just do?</p> <pre><code>class BatchRecord &lt; ActiveRecord::Base belongs_to :user def current_user self.user.class.current_user end end </code></pre> <p>In which case it would be trivial to test. BUT! WTF are you defining <code>current_user</code> 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 <code>current_user</code> for ALL <code>User</code> instances. Now another user with admin privileges logs in and <code>current_user</code> 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!</p> <p>What I reccomend doing in this case is to either making a new controller method <code>current_user</code> which returns the current user's User instance. You can also go one step further and create a wrapper model like:</p> <pre><code>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 </code></pre> <p><strike>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 <code>User.current_user &amp;&amp; User.current_user.id</code> will return a boolean, rather than the Integer you want it to.</strike> <strong>EDIT</strong> I'm an idiot.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload