我有三个模型,每个模型都有以下关联:classModel1:model1#willthiswork?isthereanywayaroundthis?endclassModel3:model1#willthiswork?isthereanywayaroundthis?end正如您在评论文本中看到的那样,我已经提到了我需要的内容。 最佳答案 您只需创建访问它的方法classModel2或者,您可以将model3s方法委托(delegate)给model1classModel2:model1end
我有以下设置classPlayer:playerdodefin_handfind_all_by_location('hand')endendendclassCard:cardsend这意味着以下工作:p=Player.find(:first)c=p.cards[0]p.score#=>2c.player.score#=>2p.score+=1c.player.score#=>3c.player.score+=2p.score#=>5但下面的行为不一样:p=Player.find(:first)c=p.cards.in_hand[0]p.score#=>2c.player.score#=
是否可以将方法委托(delegate)给Rails中的has_many关联,并且仍然将预加载的数据保存在该关联上,同时遵循得墨忒耳法则?目前在我看来,你被迫选择一个或另一个。也就是说:通过不委托(delegate)来保留预加载的数据,或者丢失预加载的数据和委托(delegate)。示例:我有以下两个模型:classUser注意:User#all_blogs_have_title?做的事情与all_have_title?的委托(delegate)方法完全相同。据我了解,以下内容违反了得墨忒耳定律。但是:它会保留您预加载的数据:user=User.includes(:blogs).firs
我今天正在阅读有关自引用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
我正在尝试将每个播放列表的第一首歌曲加入到播放列表数组中,但我很难找到有效的解决方案。我有以下模型:classPlaylist:playlist_songsendclassPlaylistSong:playlist_songsend我想得到这个:playlist_name|song_name----------------------------chill|babyfun|bffs我很难找到一种有效的方法来通过连接来做到这一点。更新****ShaneAndrade引导我朝着正确的方向前进,但我仍然无法得到我想要的。这是我所能得到的:playlists=Playlist.where('i
我最近写了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
我想知道我可以在多大程度上使用Rails中的关联。考虑以下因素:classUser:providerendclassProvider:businessesbelongs_to:userendclassBusiness我能够设置这些漂亮的快捷方式,例如User.businesses和Provider.bids但如何设置User.bids?是否可以关联一个协会,可以这么说? 最佳答案 这是完全可能的,但需要一些额外的工作。以下模型定义与nested_has_manyplugin结合使用您只需使用@user.bids即可获取属于某个用户的
我正在尝试在我的数据库中为具有belongs_to关系的模型添加一个新条目。我有2个模型,工作和客户。很容易找到有关如何在这两者之间建立关联(使用has_many和belongs_to)的教程,但我似乎找不到任何实际使用关联的示例。在我的代码中,我正在尝试为第一个客户创建一个新工作。jobs模型有一个client_id属性,我知道我可以手动填充该属性,但必须有一些ruby约定才能轻松完成此操作。Job.create(:client_id=>1,:subject=>"Test",:description=>"Thisisatest")我可以很容易地将它放入我的代码中,但我觉得ruby
我有以下代码(有点简化......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_
设置: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是在一年多以前发表的,但我只需要作者记录,其中所