草庐IT

associations

全部标签

ruby-on-rails - 将方法委托(delegate)给 has_many 关联会忽略预加载

是否可以将方法委托(delegate)给Rails中的has_many关联,并且仍然将预加载的数据保存在该关联上,同时遵循得墨忒耳法则?目前在我看来,你被迫选择一个或另一个。也就是说:通过不委托(delegate)来保留预加载的数据,或者丢失预加载的数据和委托(delegate)。示例:我有以下两个模型:classUser注意:User#all_blogs_have_title?做的事情与all_have_title?的委托(delegate)方法完全相同。据我了解,以下内容违反了得墨忒耳定律。但是:它会保留您预加载的数据:user=User.includes(:blogs).firs

ruby-on-rails - 自引用问题 has_many :through associations in Rails

我今天正在阅读有关自引用has_many:through数据情况的文章,因为我正在尝试构建一个使用它们的Rails应用程序。我找到了这个examplesituation在互联网上,我对此有疑问。让我从这个人的博客中发布这个示例代码:create_table:animalsdo|t|t.string:speciesendcreate_table:huntsdo|t|t.integer:predator_idt.integer:prey_idt.integer:capture_percentendclassAnimal'predator_id',:class_name=>'Hunt',:d

ruby-on-rails - 通过关联从具有多个关联中获取第一个关联

我正在尝试将每个播放列表的第一首歌曲加入到播放列表数组中,但我很难找到有效的解决方案。我有以下模型:classPlaylist:playlist_songsendclassPlaylistSong:playlist_songsend我想得到这个:playlist_name|song_name----------------------------chill|babyfun|bffs我很难找到一种有效的方法来通过连接来做到这一点。更新****ShaneAndrade引导我朝着正确的方向前进,但我仍然无法得到我想要的。这是我所能得到的:playlists=Playlist.where('i

ruby - 为 API 包装器实现类似 ActiveRecord 的关联

我最近写了ParseResource,它是Parse.com's的RubyAPI包装器REST接口(interface)。下面是一些基本用法:classPost"Helloworld",:author=>"Alan",:body=>"ipsolorem")这个项目还很年轻,我真正想要实现的一个功能是关联。像这样:classAuthor"Alan",:email=>"alan@example.com")p=Post.create(:title=>"Associated!",:body=>"ipsolorem",:author=>a)p.author.class#=>Authorp.aut

ruby-on-rails - rails has_many :through has_many :through

我想知道我可以在多大程度上使用Rails中的关联。考虑以下因素:classUser:providerendclassProvider:businessesbelongs_to:userendclassBusiness我能够设置这些漂亮的快捷方式,例如User.businesses和Provider.bids但如何设置User.bids?是否可以关联一个协会,可以这么说? 最佳答案 这是完全可能的,但需要一些额外的工作。以下模型定义与nested_has_manyplugin结合使用您只需使用@user.bids即可获取属于某个用户的

ruby-on-rails - rails 上的 ruby : Creating a model entry with a belongs_to association

我正在尝试在我的数据库中为具有belongs_to关系的模型添加一个新条目。我有2个模型,工作和客户。很容易找到有关如何在这两者之间建立关联(使用has_many和belongs_to)的教程,但我似乎找不到任何实际使用关联的示例。在我的代码中,我正在尝试为第一个客户创建一个新工作。jobs模型有一个client_id属性,我知道我可以手动填充该属性,但必须有一些ruby​​约定才能轻松完成此操作。Job.create(:client_id=>1,:subject=>"Test",:description=>"Thisisatest")我可以很容易地将它放入我的代码中,但我觉得ruby

ruby-on-rails - 如何使用不唯一的 "association_type"和 "association_id"在 Rails 中索引多态表

我有一个名为“投票”的多态表,其中包含来自答案和问题的投票。投票user_idvoteable_idvoteable_typevalue12Answer122Answer1在这种情况下,id=2的答案有两票赞成。问题是:如何索引这个表?第一种方法:add_index:votes,[:voteable_id,:voteable_type]这是行不通的,因为重复的键值会违反唯一约束第二种方法:add_index:votes,:voteable_id,add_index:votes,:voteable_type我猜这个不会有太多性能,因为同时对id和type进行复合查询。第三种方法:add_

ruby-on-rails - Rails:ActiveRecord::HasManyThroughSourceAssociationNotFoundError:找不到源关联

我有以下代码(有点简化......create_table:signaturesdo|t|t.integer:signer_idt.integer:card_idt.timestampsend模型看起来像......classSignature:signatures,:foreign_key=>"card_id"endclassUser"Card",:foreign_key=>"sender_id"has_many:received_cards,:class_name=>"Card",:foreign_key=>"recipient_id"has_many:signatureshas_

ruby-on-rails - rails : Return only objects where all associations meet a condition

设置:Rails3.2.18、Postgres我有两个对象,例如,将它们称为Author和Article,具有以下设置:classAuthorhas_many:articles...endclassArticlebelongs_to:authorclass我正在尝试查找所有Author记录,其中所有相关的Article记录都是在一年前发布的。这段代码:Author.joins(:article).merge(Article.published_over_one_year_ago)...返回Author对象,其中至少一个关联的Article是在一年多以前发表的,但我只需要作者记录,其中所

ruby-on-rails - 无法在 Rails 3 中找到 has_many 的逆关联

我有以下模型:classBusiness:businesshas_many:payments,:inverse_of=>:businessendclassCustomer:customerhas_many:payments,:inverse_of=>:customerendclassPayment:paymentbelongs_to:business,:inverse_of=>:paymentend执行business.customers效果很好。但是,当我执行business.payments时,出现错误:Couldnotfindtheinverseassociationforbus