我有一个方法如下......
async
我一直收到错误"错误的参数数量(0表示1)"在"采取"线。我使用rails 4.0.1是导致问题还是我错过了什么?
的文档我将方法更新为
using System;
using System.Windows;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections.Concurrent;
// sample stock quote project implementing multiple async Tasks
// and various Dictionary objects to store results
namespace QuoteServer
{
public partial class MainWindow : Window
{
// quote web service root Url
private const string _baseUrl = @"http://finance.yahoo.com/d/quotes.csv?f=l1&s=";
/// <summary>
/// Sample List of 30 Stock Symbols (Dow Jones Industrial Average)
/// </summary>
List<string> ListDJIA = new List<string>
{
"AAPL", "AXP", "BA", "CAT", "CSCO", "CVX", "DD", "DIS", "GE", "GS",
"HD", "IBM", "INTC", "JNJ", "JPM", "KO", "MCD", "MMM", "MRK", "MSFT",
"NKE", "PFE", "PG", "TRV", "UNH", "UTX", "V", "VZ", "WMT", "XOM"
};
// store results (multiple stock Px) of asynchronous Tasks in various Dictionary objects
private Dictionary<string, double> _dQuote = new Dictionary<string,double>();
private ConcurrentDictionary<string, double> _cdQuote = new ConcurrentDictionary<string,double>();
private SortedDictionary<string, double> _sdQuote = new SortedDictionary<string,double>();
private CancellationTokenSource _CTS;
public MainWindow(){ InitializeComponent();}
/// <summary>start multiple async Tasks to get quote list</summary>
private async void startButton_Click(object sender, RoutedEventArgs e) {
_CTS = new CancellationTokenSource();
try{
await AccessWebAsync(_CTS.Token);
resultsTextBox.Text += "\r\nDownloads complete.";
}
catch (OperationCanceledException){
resultsTextBox.Text += "\r\nDownloads canceled.\r\n";
}
catch { resultsTextBox.Text += "\r\nDownloads failed.\r\n"; }
finally { _CTS = null; }
}
/// <summary>cancel async Tasks</summary>
private void cancelButton_Click(object sender, RoutedEventArgs e) {
if (_CTS != null) _CTS.Cancel();
}
/// <summary>
/// access web service in async mode to run multiple Tasks
/// </summary>
/// <param name="CT">CancellationToken</param>
/// <returns>Task</returns>
async Task AccessWebAsync(CancellationToken CT)
{
_dQuote.Clear();
_cdQuote.Clear();
_sdQuote.Clear();
resultsTextBox.Clear();
HttpClient _httpClient = new HttpClient();
List<Task> _taskList = new List<Task>();
foreach (string _symbol in ListDJIA)
{
_taskList.Add(GetQuoteAsync(_symbol, _httpClient, CT));
}
await Task.WhenAll(_taskList);
// test if count is 30 (DJIA stocks)
resultsTextBox.Text += String.Concat("Dictionary Items: ", _dQuote.Count, Environment.NewLine);
resultsTextBox.Text += String.Concat("Dictionary Items: ", _cdQuote.Count, Environment.NewLine);
resultsTextBox.Text += String.Concat("Dictionary Items: ", _sdQuote.Count, Environment.NewLine);
}
/// <summary>
/// Get quote from web in async mode
/// </summary>
/// <param name="Symbol">string</param>
/// <param name="client">HttpClient</param>
/// <param name="CT">CancellationToken</param>
/// <returns>Task</returns>
async Task GetQuoteAsync(string Symbol, HttpClient client, CancellationToken CT) {
try {
HttpResponseMessage _response = await client.GetAsync(_baseUrl + Symbol, CT);
// bytes array
byte[] _arrContentBytes = await _response.Content.ReadAsByteArrayAsync();
// bytes array to string
string quote = System.Text.Encoding.UTF8.GetString(_arrContentBytes);
// try parse Stock Px as double
double _dbl;
if (double.TryParse(quote,out _dbl))
{
// add item to dictionary (simplest option)
_dQuote.Add(Symbol, _dbl);
// add item to sorted dictionary (desirable option)
_sdQuote.Add(Symbol, _dbl);
// add item to concurrent dictionary (guaranteed thread-safe option)
_cdQuote.TryAdd(Symbol, _dbl);
}
}
catch { throw; }
}
}
}
现在我收到了错误
def self.get(code)
where(code: normalize_code(code)).
where('coupon_count > 0').
where('expires_at > Time.now OR expires_at IS NULL').
take
end
错误发生在&#34; take&#34;线
-UPDATE -
我的错误在于coupon_count的where方法。它不在take方法中。我必须弄清楚在接受优惠券之前它不会检查coupon_count字段。
答案 0 :(得分:1)
http://apidock.com/rails/ActiveRecord/FinderMethods/take文档似乎说take
默认将返回限制为1.
Person.take # returns an object fetched by SELECT * FROM people LIMIT 1
我
然而,错误消息告诉我take
需要参数。查看下面这个问题(Arrays in Ruby: Take vs Limit vs First)中答案的注释,它基本上总结了在Ruby 1.8.7,1.9.3,2.0.0或2.1.0中不能在没有参数的情况下调用Take。
答案 1 :(得分:0)
由于您只选择一个,所以为什么不尝试先使用。