gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:28 25 4
gpt4 key购买 nike

我想在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com