我想知道从has_many中的连接模型中选择属性的最简单/最优雅的方法是什么:通过关联。假设我们有具有以下Item类的Items、Catalogs和CatalogItems:classItem:catalog_itemsend此外,假设CatalogueItems有一个位置属性,并且在任何目录和任何项目之间只有一个CatalogueItem。检索位置属性的最明显但有点令人沮丧的方法是:@item=Item.find(4)@catalog=@item.catalogs.first@cat_item=@item.catalog_items.first(:conditions=>{:cata
我有三个ActiveRecord模型:Partner、MembershipChannel(这是一个STI模型,继承自Channel)和ChannelMembership(我不负责命名这些模型……)当我通过合作伙伴协会加载ChannelMembership时,我有时(!)以只读记录结束。这是在Rails3.0.9中。相同的代码在2.3.11中没有这种行为。>p=Partner.first>p.channel_memberships.map(&:readonly?)#=>[false,false,false,false,false,false]>p.reload.channel_membe
我正在尝试找到一种优雅的方式来保存约会模型(如下)中称为描述的附加字段。我的模型是这样设置的:classPhysician在我看来,我有复选框设置来保存连接表的数据,但我想滑入一个额外的“描述”字段以与连接一起保存。以下是我的看法:Patients 最佳答案 您可以使用accepts_nested_attributes_for来更新关联属性。在模型中:accepts_nested_attributes_for:appointments,:allow_destroy=>true在View中:引用http://api.rubyonrai
我有一个管理借方和贷方的发票系统。基本上,发票金额是通过其借方总和得出的,余额是通过将其贷方总和减去总金额得出的。我用四个模型来做这个。发票订单项借方信用它的工作方式是通过一个连接模型(行项目),该模型具有一个称为可记录的多态关联。乍一看一切似乎都正常工作。但是,检查行项目表明虽然recordable_id正常显示,但recordable_type为nil。下面是代码的分解:classInvoice:destroyhas_many:debits,:through=>:line_items,:as=>:recordablehas_many:credits,:through=>:line_
我正在使用StringScanner进行词法分析,如下所示:defnext@scanner.skip(/\s+/)value,kind=nil,nilTOKEN_DEF.each{|tok,regex|(kind=tok;break)if@scanner.scan(regex)}returnToken.new(kind,value,@line,@scanner.pos)end初步估计,这很好用,只是我不知道现在如何获取@line编号。我已经阅读了文档,begin_of_line在哪里?方法似乎合适,但我不知道如何使用它。 最佳答案
我正在构建一个应用程序来为StarWarsRPG生成角色。我添加了Species模型。Species既是单数又是复数,这让我陷入了一个循环。我可以通过使用species_index_path让路由在应用程序中正常工作,但由于某些原因,has_many:characteristics,:through=>:species_characteristics无法正常工作一个方向。例如,我有两个模型在控制台中正确播种(并工作):Characteristics和Species。Characteristics设置如下:classCharacteristic:species_characteristi
在osx10.9mavericks上运行geminstallnokogiri-v'1.6.1'获得:make"DESTDIR="cleanmake"DESTDIR="compilinghtml_document.ccc1:error:unrecognizedcommandlineoption"-Wdivision-by-zero"make:***[html_document.o]Error1makefailed,exitcode2 最佳答案 当我尝试在Mac10.9上安装nokogiri1.6.6.2时遇到了这个错误,我是这样修复的
我有一个类似这样的数据模型:#columnsincludecollection_item_id,collection_id,item_id,position,etcclassCollectionItem:collection_items,:source=>:collectionendclassCollection:positionhas_many:items,:through=>:collection_items,:source=>:item,:order=>:positionend一个项目可以出现在多个集合中,也可以在同一个集合中的不同位置出现不止一次。我正在尝试创建一个辅助方法来创
我有这个关联: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
我正在尝试在我的Rails2.3.8应用程序(ruby1.8.7)中模拟twitter模型classConnection'subject_id',:primary_key=>'user_id',:class_name=>'User'belongs_to:follower,:foreign_key=>'follower_id',:primary_key=>'user_id',:class_name=>'User'endclassUser'user_id',:foreign_key=>'follower_id',:class_name=>'Connection'has_many:relat