51224bdd17878b3b19e8987e9bb336a2

Hi Following is the code snippet
- I am just fetching users who have age more then current user age, I am doing same thing in 3 actions.
- Is there any way to keep @users variable persistent for 3 actions. or for full controller. and also my database should not get hit more than once.
- I also tried using class inheritable attributes but No success.

Thanks
DG

class MyaccountController < ApplicationController
    
  def account_details
    @users = User.find(:all,:conditions=>"age > #{current_user.age}")
  end
  
  def show_email_box
    @users = User.find(:all,:conditions=>"age > #{current_user.age}")
  end
  
  def show_mobile_box
    @users = User.find(:all,:conditions=>"age > #{current_user.age}")
  end
  
end

Refactorings

No refactoring yet !

Df2e97f18b5802e199d4920552a52d34

rafeco

July 31, 2008, July 31, 2008 13:31, permalink

1 rating. Login to rate!

You could create a before_filter to DRY up the code a bit, as shown in the refactoring. Trying to cache the users so you only have to hit the database once (for some unspecified period of time) seems fraught with risk.

class MyaccountController < ApplicationController

  before_filter load_users, :only => [ :account_details, :show_email_box, :show_mobile_box ]
    
  def account_details
  end
  
  def show_email_box
  end
  
  def show_mobile_box
  end

  protected 
  
  def load_users
    @users = User.find(:all,:conditions=>"age > #{current_user.age}")
  end
end
Be1e3ee645d23c95ba650c21bc885927

Fabien Jakimowicz

July 31, 2008, July 31, 2008 13:33, permalink

1 rating. Login to rate!

You can use a before filter to avoid code duplication.

But keeping 'dynamic' data between queries is not always a good idea : data can change between 2 queries and you can have inconsistent data.

Anyway, you can use session to store data between queries. Here are 2 solutions.

You can even remove action definition if there are empty, but you will loose code clarity

class MyaccountController < ApplicationController
  before_filter :load_users, :only => [ 'account_details', 'show_email_box', 'show_mobile_box' ]
    
  def account_details
  end
  
  def show_email_box
  end
  
  def show_mobile_box
  end

  private
  def load_users
    @users = User.find(:all,:conditions=>"age > #{current_user.age}")
  end
  
end
class MyaccountController < ApplicationController
  before_filter :load_users, :only => [ 'account_details', 'show_email_box', 'show_mobile_box' ]
    
  def account_details
  end
  
  def show_email_box
  end
  
  def show_mobile_box
  end

  private
  def load_users
    session[:users] ||= User.find(:all,:conditions=>"age > #{current_user.age}")
    @users = session[:users]
  end
  
end
880cbab435f00197613c9cc2065b4f5a

danielharan

July 31, 2008, July 31, 2008 13:51, permalink

1 rating. Login to rate!

Persistent storage by adding it to the session smells. Take Fabien's first solution to avoid code duplication. If you really are facing performance issues, then look in to caching the front-end (assuming you're keeping the list of @users in a partial).

1f393f50dc29119d656c91d6701ddbba

Jeremy Weiskotten

July 31, 2008, July 31, 2008 14:21, permalink

1 rating. Login to rate!

If you're using Rails 2.1, I also like using scopes. This would be overkill if you don't need the logic in more than one controller, but it's an expressive way to encapsulate commonly-used queries... any they can be combined with each other, such as User.older_than(me).male.with_hair('brown')

class User
  named_scope :older_than, lambda { |u| { :conditions => ['age > u', u.age] } }
end


@users = User.older_than current_user
8484df61a1434e91cb088f53cc089f18

Adam

August 11, 2008, August 11, 2008 18:32, permalink

No rating. Login to rate!

I'm not sure exactly what you are trying to do with all those actions, but from what has been given it feels like it might be appropriate to treat them as alternate content types rather than separate actions.

class UsersController < ApplicationController
  # /users        => app/views/users/users.html.erb
  # /users.email  => app/views/users/users.email.erb
  # /users.mobile => app/views/users/users.mobile.erb
  def index
    @users = User.older_than(current_user.age)
  end  
end
Mime::Type.register_alias "text/html", :mobile
Mime::Type.register_alias "text/html", :email
8484df61a1434e91cb088f53cc089f18

Adam

August 11, 2008, August 11, 2008 18:35, permalink

No rating. Login to rate!

This site needs an edit function. That should be index.html.erb, index.email.erb, etc.

Your refactoring





Format Copy from initial code

or Cancel