我想要在視圖中顯示所有不同的記錄。我在我的accounts_controler.rb文件@account = Account.count("created_at", :distinct => true)
中有以下內容,或者我應該使用Account.distinct.count('date')
?如果我正確地讀了導軌4 :distinct => true
現已棄用如何計算Ruby on Rails應用程序中的記錄?
我不確定這是否正確。這裏是整個控制器文件:
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
我beleive應該有一個紅寶石標籤包括在index.html.erb文件,但我不知道語法。
有什麼問題嗎? –
問號grep – inger
我從這個問題中學到了一些新東西,+1。 @FrederickCheung,它是如何計算不同的字段 –