草庐IT

known_associations

全部标签

ruby-on-rails - 事件记录 :includes - how to use map with loaded associations?

我有一个小型Rails应用程序,我正在尝试获取一些订单统计信息。所以我有一个Admin模型和一个Order模型,具有一对多关联。classAdmin我正在尝试使用此查询获取特定订单:admins=Admin.where(...).includes(:orders).where('orders.operation=?','newgifts!')正如预期的那样工作。但是当我尝试像那样使用map制作json时admins.map{|a|[a.name,a.orders.pluck(:operation)]}Rails使用新查询再次加载订单,忽略已经加载的对象。(5.6ms)SELECT"or

ruby-on-rails - belongs_to touch : true not fired on association. 构建和嵌套属性赋值

在OpenProject应用我们有两种模型:CustomFieldclassCustomField{order(position::asc)},dependent::delete_allaccepts_nested_attributes_for:custom_options...endCustomOptionclassCustomOption然后在customfieldcontroller我们通过批量分配修改自定义字段的选项并保存记录:@custom_field.attributes=get_custom_field_paramsif@custom_field.save...因为tou

ruby-on-rails - 如何在不调用依赖 : :destroy on associations 的情况下销毁 Rails 模型

有没有一种方法可以销毁Rails模型,而无需为关联中的dependent::destroy调用回调。例子:classAdministration所以当我打电话Administration.find(id).destroy我只想删除记录和附件,但不要调用删除rosters的回调has_many:rosters,dependent::destroy--PS我不想禁用has_many:rosters,dependent::destroy。我只需要暂时禁用回调。 最佳答案 您可以保持关联不变,并通过以下方式之一跳过回调:1.使用delete

ruby-on-rails - rails : how to eager load limited records of ordered association without N+1 queries

我知道关于其中一些主题有很多问题,但我没有找到涵盖所有方面的问题。考虑User、Activity和Like模型。当我查询一个事件时,我想为集合中的每个事件加载第一个Like而不进行N+1查询并且不加载超过必要的记录。我的代码看起来像这样:classUser{order(created_at::asc)},class_name:"Like"endclassLike我做了一个全面的要点来测试不同的加载策略和方法:https://gist.github.com/thisismydesign/b08ba0ee3c1862ef87effe0e25386267策略:N+1查询、左外连接、单次额外查

ruby - 在多个线程中运行时出现 "Name or service not known (SocketError)"错误

我制作了一个并行解析文本文件和下载数据的程序。在9个或更少的线程中运行下载方法时,程序没有错误。但是当在10个或更多线程中运行该方法时,程序会抛出“`initialize':getaddrinfo:Nameorservicenotknown(SocketError)”错误。我尝试了一些算法并行运行,但出现了同样的问题。我将发生“名称或服务未知”错误时传递给“打开”方法(open-uri)的url放入浏览器,并确认该url有效并收到正确的数据。这是部分代码。jobs=[]aps=[]....#jobsarepushedintojobs[]....max_thread=15loopdoar

ruby-on-rails - 在 has_many :through association, 用 lambda 替换条件

我有这个关联:has_many:foo_participators,through::foos,source::user,conditions:"foos.state='completed'"Rails告诉我:DEPRECATIONWARNING:ThefollowingoptionsinyourBar.has_many:foo_participatorsdeclarationaredeprecated::conditions.Pleaseuseascopeblockinstead.Forexample,thefollowing:has_many:spam_comments,condi

ruby-on-rails - rails 模型 has_many :through associations

我正在努力解决我的人际关系,但我在使用关联时遇到了问题。所以我有三个模型Workout、Exercise和WorkoutExercise。一个锻炼应该有很多练习,一个锻炼应该有不同的锻炼,因此我写道:classWorkout:workout_exercisesendclassExercise:workout_exercisesendclassWorkoutExercise我正在运行一些测试,但是一旦我创建了一个锻炼、锻炼然后​​将它们加入到workout_exercise类中,这些测试就没有通过。它不会让我像这样访问锻炼中的练习:Workout.createExercise.creat

c++ - 关于c++转换: no known conversion for argument 1 from ‘[some_class]' to ‘[some_class]&’

我正在使用C++,但出现了一个我不知道确切原因的错误。我找到了解决方案,但仍然想知道为什么。classBase{public:voidsomething(Base&b){}};intmain(){Baseb;b.something(Base());return0;}当我编译代码时,出现以下错误:abc.cpp:12:20:error:nomatchingfunctionforcallto‘Base::something(Base)’abc.cpp:12:20:note:candidateis:abc.cpp:6:7:note:voidBase::something(Base&)abc.

c++ - 关于c++转换: no known conversion for argument 1 from ‘[some_class]' to ‘[some_class]&’

我正在使用C++,但出现了一个我不知道确切原因的错误。我找到了解决方案,但仍然想知道为什么。classBase{public:voidsomething(Base&b){}};intmain(){Baseb;b.something(Base());return0;}当我编译代码时,出现以下错误:abc.cpp:12:20:error:nomatchingfunctionforcallto‘Base::something(Base)’abc.cpp:12:20:note:candidateis:abc.cpp:6:7:note:voidBase::something(Base&)abc.

ruby-on-rails - rails : Serializing deeply nested associations with active_model_serializers

我正在使用Rails4.2.1和active_model_serializers0.10.0.rc2我是API新手并选择了active_model_serializers,因为它似乎正在成为rails的标准(尽管我不反对使用RABL或其他序列化程序)我遇到的问题是我似乎无法在多级关系中包含各种属性。例如,我有:项目classProjectSerializer和估计classEstimateSerializer提案classProposalSerializer当我点击/projects/1时,上面会产生:{"id":1,"name":"123ParkAve.","updated_at":