我想在表格中获得十几列的总和.
# result will be {:a => 340.5, :b => 21.8, ... }
# where :a has the sum of the :a column values
# entries is an ActiveRecord model, e.g. ScoreCard.where(:user => user)
def self.totals(entries)
[:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :m].inject({}) do |tv, col|
tv[col] = entries.sum(col)
tv
end
end
Run Code Online (Sandbox Code Playgroud)
有没有办法在一个查询中执行此操作?以上生成了十几个查询.
如果您已经成功测试了使用门卫 OAuth2提供商gem 保护的Rails API的post,put和delete方法,请分享,我会给你爱.
该看门wiki文档和示例应用程序显示非常好如何测试get方法.我成功地测试了一个帖子,其中包括使用Cuyumber的Capybara测试驱动程序.无法测试从put或delete路由的任何API.无法使用rspec测试发布.
@user = create :user
@client = create(:oauth_application)
@token = create(:oauth_token, :application => @client, :resource_owner_id => @user)
json_for_new_entry = {
date_attr: Time.now.to_date,
decimal_attr: '1.1',
string_attr: 'oath2, you make me blue',
bool_attr: false,
int_attr: 1
}.to_json
page.driver.header 'Authorization', "Bearer #{@token.token}"
page.driver.post api_entry_path, json_for_new_entry,
'CONTENT_TYPE' => 'application/json'
Run Code Online (Sandbox Code Playgroud)
工厂没什么特别的:
factory :user, :class => User do |user|
sequence :email do |n| "user#{n}@example.com" end
pwd = "password"
password pwd
end
factory :oauth_application, :class => Doorkeeper::Application do …Run Code Online (Sandbox Code Playgroud)