user646560
user646560

Reputation:

Rails 3: Where to write a non model specific method to be called in the controller?

I know it might sound like a stupid question, but for my index method in my record controller i dont want to return all objects of record, some logic is applied to determine which records are displayed depending on the user logged in.

Where would i keep such a method that would take the user as a paramater and return an array of the records to be displayed in the index method and how would it be called? in the controller?

so it would be something like

def index
  @records = # get_records method would be called here
end

thanks

Upvotes: 0

Views: 188

Answers (2)

moritz
moritz

Reputation: 25757

If the logic is more complex, I suggest, you put it into a (named) scope within the model. So for Rails 3, you would do:

class Record < ActiveRecord::Base
  scope :some_name, lambda {|user|
    # apply your logic here
  }

  ...
end

Upvotes: 1

Mando Escamilla
Mando Escamilla

Reputation: 1560

There's no reason why you couldn't (or shouldn't) put that logic in the index method of the controller, such as:

def index
  user_id = params['user']
  @records = Record.find_by_user(user_id)
end

Upvotes: 3

Related Questions