1

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
prabu
  • 6,121
  • 8
  • 35
  • 39

1 Answers1

2

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.

whizcreed
  • 2,700
  • 3
  • 22
  • 35
  • I have the similar usecase where when the user load page I want to show all the rows in which Is_active is true but I also want to show the is_active false rows if the user searches for them explicitly . I am unable to show it . can you please help me here . – shruthi chowdhary May 17 '23 at 09:36