草庐IT

polymorphic-associations

全部标签

ruby-on-rails - Elasticsearch /轮胎 : How to map to association attribute?

我正在使用Tire用于Elasticsearch。在我的应用程序中,我有2个模型;价格和产品。我正在尝试搜索我的Price类并使用它所属的Product的:name属性作为搜索字段。现在,如果我有一个名为Product1的产品并输入“pro”、“prod”或“duct”,则不会出现任何结果。但是键入“product”或“Product”会显示结果。我相信问题出在我的映射上。我查看了查询及其:...localhost:3000/search/results?utf8=%E2%9C%93&query=product当我认为应该是:...localhost:3000/search/resul

ruby-on-rails - rails : Order a model by the last element of a has_many association

我有两个模型:User和Message,它们通过has_many关系连接。我想获得一个users列表,按他们上一条消息的时间戳排序。classUser当我这样做时:@users=User.includes(:messages).order('messages.created_atDESC').limit(5)它似乎对消息进行排序,获取最新的5条消息,然后返回与这些消息关联的用户。所以用户数可以少于5个。我想确保获得5个用户。我希望查询为每个请求获取最新消息,对最后的消息进行排序,然后将最新消息返回给用户。所以我想要这样的东西:@users=User.includes(:messages

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 4 : polymorphic set base class type instead of inherited

我对Rails关系有疑问。我有他的继承版本的基本模型classUser接下来我有一个多态关联的成员模型classMembership当我尝试创建成员资格模型的新实例时,例如键入Membership.newgroup:Group.first,membershipable:Admin.firstmembershipable_type设置为“User”而不是“Admin”。所以我创建了before_validation回调defproper_sti_typeself.membershipable_type=memebrshipable.class.nameend它有效,但我想这是更好的方法。

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 - Rails 3 : Polymorphic liking of Entities by User, 怎么办?

背景:我遵循了教程here在我的应用程序中设置多态用户收藏夹数据模型。这允许我让用户在系统中创建几乎所有实体,我将“has_many:favorites,:as=>:favorable”行添加到它的模型收藏夹。我计划使用它来实现Facebook风格的“赞”系统以及其他几个类似的系统。首先,我将好感度添加到Post模型(每个用户都可以像在Facebook上一样创建状态更新)。我已经完成了所有工作并进行了单元测试,所以我知道数据模型在关系的任何一方(用户和帖子)都是合理的和有效的。详细信息:我有一个带有单一索引方法和View的HomeController。在索引View中,我为用户和用户的

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