草庐IT

association_chain

全部标签

ruby-on-rails - has_many autosave_associated_records_for_*

我正在尝试关联现有记录,同时仍然能够添加新记录。以下不起作用,但非常接近我需要的。如何完成关联现有记录和创建新记录?has_many:comments,:through=>:commentings,:source=>:commentable,:source_type=>"Comment"accepts_nested_attributes_for:comments,:allow_destroy=>truedefautosave_associated_records_for_commentscomments.eachdo|comment|ifexisting_comment=Comment

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-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 - alias_method 和 alias_method_chain 有什么区别?

我正在开发我的网络应用程序,我想覆盖一个方法,例如,如果原始类是classAdeffoo'original'endend我想重写foo方法,可以这样做classAalias_method:old_foo,:foodeffooold_foo+'andanotherfoo'endend我可以像这样调用旧方法和新方法obj=A.newobj.foo#=>'originalandanotherfoo'obj.old_foo#=>'original'那么,如果我可以像以前那样访问和保留这两种方法,那么alias_method_chain有什么用呢? 最佳答案

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

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

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":

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":