草庐IT

many-subdirs

全部标签

ruby-on-rails - Ruby on Rails 在保存之前通过关联对象 has_many

在RubyonRails项目中,我试图在将所有内容保存到数据库之前访问ActiveRecord上的关联对象。classPurchase如果我像这样构建我的对象:purchase=Purchase.new(item_ids:[1,2,3])并尝试保存它item_validation方法还没有填充项目集合,所以即使虽然项目已经设置,但它没有机会调用任何项目的check_something方法。是否可以在我的购买模型和关联模型保留之前访问项目集合,以便我可以对它们运行验证?如果我将item_validation方法更改为:defitem_validationpurchase_items.ea

ruby-on-rails - 通过关联获取 has_many 的 ActiveRecord::RecordInvalid 错误;连接表上的验证问题

我有如下三个关联模型:classProduct当我做类似的事情时:doc=user.documents.builddoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到false和doc上的相应错误。这正是我想要的。但是,如果doc已经存在,例如:doc=user.documents.firstdoc.update_attributes(:product_ids=>[1,2])并且description验证失败,然后我得到一个ActiveRecord::RecordInvalid错误。我很清楚为什么会这样——i

ruby-on-rails - 设置一个 :has_many :through association on a belongs_to association Ruby on Rails

我有三个模型,每个模型都有以下关联:classModel1:model1#willthiswork?isthereanywayaroundthis?endclassModel3:model1#willthiswork?isthereanywayaroundthis?end正如您在评论文本中看到的那样,我已经提到了我需要的内容。 最佳答案 您只需创建访问它的方法classModel2或者,您可以将model3s方法委托(delegate)给model1classModel2:model1end

ruby - Rails 3 Order By Count on has_many :through

我有一个应用程序,我可以在其中列出项目并为每个项目添加标签。模型Items和Tags是这样关联的:classItem:taggingsendclassTagging:taggingsend因此,这种多对多关系允许我为每个Item设置n标签,并且同一个标签可以多次使用。我想列出所有标签,这些标签按与该标签关联的项目数排序。更多使用的标签,首先显示。较少使用,最后。我该怎么做?问候。 最佳答案 Tag.joins(:taggings).select('tags.*,count(tag_id)as"tag_count"').group(:

ruby-on-rails - mongoid中的has_many、has_and_belongs_to_many和embeds_many到底有什么区别?

我知道这不是编程问题,我找不到非常清晰和描述性的解决方案。 最佳答案 Mongoid的documentation很清楚:Embeddedrelationsdescribedocumentswhoarestoredinsideotherdocumentsinthedatabase.Referencedrelationsdescribedocumentsthatreferencedocumentsinanothercollectionbystoringforeignkeydata(usuallyanid)abouttheotherdoc

ruby-on-rails - 使用 has_many :through and build

我有三个模型,都用于has_many:through关系。它们看起来像这样:classCompany这加载很好,并且连接是为查询而构建的。但是,每当我做类似的事情时@company=Company.last@user=@company.users.build(params[:user])@user.save#=>true@company.save#=>trueUser记录和CompanyUser记录被创建,但是company_idCompanyUser中的字段记录设置为NULLINSERTINTO`companies_users`(`company_id`,`created_at`,`

ruby-on-rails - Has_Many :Through or :finder_sql

我已经确定了我想要的东西,但我似乎无法以Rails设计师正在寻找的方式获得它。基本上,我有(请搁置多元化/等问题):人类关系(parent、后代)我正在尝试获取单亲的所有后代,以及许多后代的单亲(假设每个后代只有一个parent)。我可以在模型中通过以下方式做到这一点:has_one:parent,:through=>:relationships,:foreign_key=>:human_id,:source=>:source_humanhas_many:offsprings,:finder_sql=>'SELECTDISTINCToffsprings.*'+'FROMhumansof

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 - has_many 嵌套表单,其中包含 has_one 嵌套表单

我目前正在尝试为一个模型制作一个表单,该模型具有动态数量的嵌套模型。我正在使用嵌套表单(如RailsCasts197中所述)。让事情变得更加复杂的是,我的每个嵌套模型都有一个与第三个模型的has_one关联,我也想将其添加到表单中。对于任何对过度规范化或不正确方法感到疑惑的人,此示例是我所面临问题的简化版本。实际上,事情稍微复杂一些,这就是我们决定采用的方法。一些示例代码来说明下面的问题:#MODELSclassTestattr_accessible:test_name,:test_description,:questions_attributeshas_many:questionsa