У меня есть следующий код в одном из моих контроллеров (в приложении Rails 3.1), который работает хорошо:
def index
#@calls = Call.all
@calls = Call.where(:destination => '12345678').limit(25)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @calls }
end
end
Я пытаюсь найти наилучший способ отсюда, в основном каждый пользователь имеет свой собственный код назначения (в данном случае это 12345678).
Возможно ли для пользователей иметь значение в модели, которое может быть передано в контроллер?
Пример
def index
#@calls = Call.all
@calls = Call.where(:destination => '<% @user.destination %>').limit(25)
respond_to do |format|
format.html # index.html.erb
format.json { render :json => @calls }
end
end
Я понимаю, что вышеприведенный код не сработает, но что будет обходным путем для достижения того же самого?
Обновление с дополнительной информацией:
У меня есть две модели, одна - звонки, а другая - пользователи.
Я хочу иметь возможность сделать что-то вроде этого:
@calls = Call.where(:destination => @user.destination_id).limit(25)'
Где: destination является частью модели Calls, а destination_id является частью модели users. У каждого пользователя есть свое значение destination_id.
Маршруты
Outofhours::Application.routes.draw do
ActiveAdmin.routes(self)
devise_for :admin_users, ActiveAdmin::Devise.config
get "log_out" => "sessions#destroy", :as => "log_out"
get "log_in" => "sessions#new", :as => "log_in"
get "sign_up" => "users#new", :as => "sign_up"
resources :users
resources :sessions
resources :calls
root :to => 'dashboards#index'
resources :dashboards
end
модель пользователя
class User < ActiveRecord::Base
attr_accessible :email, :company, :destination_id, :password, :password_confirmation
attr_accessor :password
before_save :encrypt_password
validates_confirmation_of :password
validates_presence_of :password, :on => :create
validates_presence_of :email
validates_uniqueness_of :email
validates_uniqueness_of :company
validates_uniqueness_of :destination_id
def self.authenticate(email, password)
user = find_by_email(email)
if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
user
else
nil
end
end
def encrypt_password
if password.present?
self.password_salt = BCrypt::Engine.generate_salt
self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
end
end
end
модель вызова
class Call < ActiveRecord::Base
end