作者:mobiledu2502932321 | 来源:互联网 | 2023-05-17 11:56
Ihavethefollowingcodeinmyaccount.rbmodelfile:我的账户里有以下代码。rb模型文件:classAccount<Active
I have the following code in my account.rb model file:
我的账户里有以下代码。rb模型文件:
class Account
And the following code in the index method from my accounts_controller.rb file:
以及我的accounts_controller的索引方法中的以下代码。rb文件:
def index
@accounts = Account.all
if params["page"]
page = params["page"]
items_per_page = params["per_page"]
render :json => {:total => @accounts.count,:accounts => @accounts.page(page).per(items_per_page) }
else
render json: @accounts
end
end
As expected, render json: @accounts
returns a result set that contains the alias_attribute
column names defined in the model file. However, the render :json => {:total => @accounts.count,:accounts => @accounts.page(page).per(items_per_page) }
code returns a result set that contains the original column names. Is there any way to change this so that the alias_attribute
column names are used?
如预期的那样,呈现json: @accounts返回一个结果集,其中包含模型文件中定义的alias_attribute列名。但是,呈现:json => {:total => @accounts。count,:accounts => @accounts.page(page).per(items_per_page)}代码返回一个包含原始列名的结果集。有什么方法可以更改它,以便使用alias_attribute列名吗?
1 个解决方案