我有一个模型关系,其中today
有很多tasks
我正在尝试检索用户的today
对象,包括tasks
并将它们全部呈现给Json.所有这一切都很顺利,直到我决定要tasks
在today
对象内进行排序,因为respond_with block
它也用于渲染html页面.有没有办法包括tasks
和订购它们?
我正在尝试这样的事情:
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = Today.where(:user_id => current_user.id).joins(:tasks).includes(:tasks).order(:priority).first
respond_with @today, :include => :tasks
end
end
Run Code Online (Sandbox Code Playgroud)
这会正确检索所有内容,但似乎根本不会对任务进行排序.
这就是我曾经拥有的(效果很好,但没有订购):
class TodaysController < ApplicationController
respond_to :html, :json
def show
@today = current_user.today
respond_with @today, :include => :tasks
end
end
Run Code Online (Sandbox Code Playgroud)
我知道我可以检索数据并在之后对其进行排序,如下所示:
@today = current_user.today
@today.tasks.sort!{|a,b| a.priority <=> b.priority }
Run Code Online (Sandbox Code Playgroud)
这有效并将通过我的测试,但我希望有一种ActiveRecord方法来解决这个问题.
我有一个像这样的cpp文件:
#include Foo.h;
Foo::Foo(int a, int b=0)
{
this->x = a;
this->y = b;
}
Run Code Online (Sandbox Code Playgroud)
我如何在Foo.h中引用它?
我有一个看起来像这样的文件:
A B C D E
0 8 6 12 5
8 0 10 8 9
6 10 0 7 11
12 8 7 0 6
5 9 11 6 0
Run Code Online (Sandbox Code Playgroud)
我提前不知道会有多少行和列.我想阅读顶行,它会让我知道预期的行数.我找到了lisp的(read <stream>)
函数,它在一个循环中可以将每个字符解析为符号.但是,我没有找到办法将循环限制为只读取第一行并停在那里.我正在尝试制作的解决方案就像是
(with-open-file (stream "/home/doppler/tmp/testcase1.txt")
(setf line (read-line stream))
(when line
(loop for symbol = (read line nil)
while symbol do (print symbol))))
Run Code Online (Sandbox Code Playgroud)
这个问题是(read-line stream)
返回一个无法解析的字符串(read line nil)
来提取符号(s表达式).
如何将字符串行转换为流,或者,如果可能,直接从字符串中提取符号?
我正在使用以下命令从每个数据库的每列转储100行.问题是它返回前 100条记录,而我更愿意拥有最后一条记录.我找到了一个标志,让我按主键( - order-by-primary)排序,但仍然返回错误的行.我会尝试将'order by'潜入下面的where标志,但每个表的主键是不同的.
mysqldump -u username -p --where="true limit 100" --all-databases > dump.sql
Run Code Online (Sandbox Code Playgroud)
有没有办法在抓取最后100条记录之前颠倒顺序,或者可能是引用PK而不是PK的列名称?
如何在单元测试调用的函数中模拟用户输入(使用Python 3的unittest)?例如,我有一个函数foo()
谁正在测试我的输出.在该foo()
函数中,它要求用户输入:
x = input(msg)
输出基于输入:
print("input: {0}".format(x))
我希望我的单元测试运行foo()
,输入一个输入并将结果与预期结果进行比较.
我们使用Amazon AWS Java Library上传文件,但很难获得上传进度.我们目前正在调用以下内容:
File file = new File(localAsset.getVideoFilePath());
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, localAsset.getFileName(), file);
s3.putObject(putObjectRequest);
Run Code Online (Sandbox Code Playgroud)
我们如何设置回调来检查文件上传进度?
谢谢
activerecord ×1
amazon-s3 ×1
arel ×1
bash ×1
c++ ×1
constructor ×1
header ×1
java ×1
lisp ×1
logic ×1
mysql ×1
mysqldump ×1
python-3.x ×1
ruby ×1
stream ×1
string ×1
unit-testing ×1