1

我想列出数据库中的所有记录,这些记录在我的索引页面中都是活动的(真)。

我正在使用带有 rails 2.3.8 的活动脚手架插件。任何建议如何在我的控制器中添加活动条件?

这是我的管理员控制器

   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

楷模

   class Account < ActiveRecord::Base
     has_many :customer_accounts
   end

   class CustomerAccount < ActiveRecord::Base
    belongs_to :account
   end

表结构

  create_table :customer_accounts do |t|
     t.integer :account_id
     t.active :boolean, :default=>true
     t.timestamps
  end
4

1 回答 1

2

您可以将此以下方法添加到您的控制器,以在活动脚手架集合上应用条件:

  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

下面粘贴的是解释ActiveScaffold API方法的部分:

conditions_for_collection 控制器方法

如果要在 List 使用的 find(:all) 中添加自定义条件,则定义此方法。它可以以字符串或数组语法返回条件。作为控制器上的实例方法,它可以访问参数和会话以及所有标准的好东西。

例子:

def conditions_for_collection
  ['user_type IN (?)', ['admin', 'sysop']]
end

您还可以指定连接以确保关联模型可用于以下条件:

  def joins_for_collection
    [:customer_accounts]
  end

希望这可以帮助。

于 2011-07-01T08:21:23.740 回答