ruby-on-rails - 如何统计 Ruby on Rails App 中的记录?

标签 ruby-on-rails ruby

我想在 View 中显示所有不同的记录。我的 accounts_controler.rb 文件中有以下内容 @account = Account.count("created_at", :distinct => true) 或者我应该使用 Account.distinct.count('date' )?如果我没看错,rails 4 :distinct => true is now deprecated

我不确定这是否正确。这是整个 Controller 文件:

class AccountsController < ApplicationController
  before_action :authenticate_user!
  before_action :set_account, only: [:show, :edit, :update, :destroy]

  respond_to :html

  def search
  if params[:search].present?
    @account = Account.search(params[:search])
  else
    @account = Account.all
  end
  end

  def index
    @account = Account.all.order("created_at DESC").page(params[:page]).per(10)
    @account = Account.count("created_at", :distinct => true)
  end

end

  def show
    @notes = Note.where(account_id: @account.id) #Where a note belong to the current account
  end

  def new
    @account = Account.new
    respond_with(@account)
  end

  def edit
  end

  def create
    @account = Account.new(account_params)
    @account.save
    respond_with(@account)
  end

  def update
    @account.update(account_params)
    respond_with(@account)
  end

  def destroy
    @account.destroy
    respond_with(@account)
  end

  private
    def set_account
      @account = Account.find(params[:id])
    end

    def account_params
      params.require(:account).permit(:first_name, :last_name, :return_client, :program_id, :insurance_id, :address, :phone)
    end

我相信 index.html.erb 文件中应该包含一个 ruby​​ 标记,但我不确定语法。

最佳答案

你是对的。

rails 3:

Account.count(:created_at, distinct: true)

rails 4:

Account.distinct.count('date')

关于ruby-on-rails - 如何统计 Ruby on Rails App 中的记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34799406/

相关文章:

ruby-on-rails - Webrat 说它找不到一些文本,但文本实际上在那里

ruby-on-rails - Linux 发行版中的 gem 与 gem 安装的优缺点?

ruby-on-rails - ruby rails : making id in column as an alias?

ruby-on-rails - 无论我推送什么到heroku应用程序(Ruby on Rails),内部服务器错误

ruby-on-rails - number_with_ precision 未返回 :fr locale in ruby on rails 的正确结果

ruby-on-rails - 没有这样的文件要加载-bcrypt_ext(通过devise)

ruby-on-rails - Rails 关注 class_macro 和继承类的参数

ruby-openid:执行发现时未设置@socket

ruby-on-rails - 删除重复的哈希值并根据值设置值

ruby-on-rails - Rails Rich Association - 覆盖关联模型