I want to list all the records from DB, which are all active(true) in my index page.
I'm using active scaffold plugin with rails 2.3.8. any suggestion how to add active condition in my controller?
here is my admin controller
class Admin::AccountsController < ApplicationController
active_scaffold :accounts do |config|
config.list.sorting = {:id => :asc}
config.list.columns = [:description,:year]
config.actions = [:create, :update,:list,:search,:delete]
end
end
Models
class Account < ActiveRecord::Base
has_many :customer_accounts
end
class CustomerAccount < ActiveRecord::Base
belongs_to :account
end
Table structure
create_table :customer_accounts do |t|
t.integer :account_id
t.active :boolean, :default=>true
t.timestamps
end
You can add this following method to your controller to apply conditions on the active scaffold collection:
def conditions_for_collection
unless has_role?(:admin) # if you want to limit records for non admin users
['customer_accounts.active is ?', true]
end
end
Pasted below is the part explaining the method on ActiveScaffold API:
conditions_for_collection controller method
If you want to add custom conditions to the find(:all) used by List, then define this method. It may return conditions in string or array syntax. And as an instance method on the controller, it has access to params and session and all the standard good stuff.
Example:
def conditions_for_collection
['user_type IN (?)', ['admin', 'sysop']]
end
You may also specify joins to ensure that the associated model is available to be used in conditions:
def joins_for_collection
[:customer_accounts]
end
Hope this helps.