- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 rails 4.2.0 - 这是我的模型:
class Customer < ApplicationRecord
has_many :meters, -> { includes :contracts }
end
class Meter < ApplicationRecord
belongs_to :customer
has_many :contracts
end
class Contract < ApplicationRecord
belongs_to :meter
end
现在,我想显示一个客户的所有契约(Contract)。根据Rails Guides我应该能够做到这一点:
@customer.meters.contracts
但是,它不起作用。这是错误消息:
undefined method `contracts' for #<Meter::ActiveRecord_Associations_CollectionProxy:0x007fb0385730b0>
在这种情况下最好的方法是什么?感谢您的帮助!
最佳答案
在您的示例中,每个模型类仅代表数据库记录的这些实例之一,即您的 Meter
类仅代表 中的一个
表。所以考虑到这一点,你可以看到 meter
行米meter
has_many
contracts
的声明意味着那些 meter
中的每一个code> 实例有很多 contracts
。
让我们把这一行分解:@customer.meters.contracts
您有您的@customer
实例,您要求它提供所有的计量表:@customer.meters
。该meters
方法的返回值是属于该@customer
的meters
的集合。由于它是具有多个合约的 meter
实例,因此您不能以这种方式向 meter 集合请求它们的合约。
您可以向单个仪表询问它:@customer.meters.first.contracts
或者您可以遍历所有仪表并获取所有契约(Contract):
@customer.meters.flat_map(&:contracts) # => an array of all the contracts
或者,更一般地说,您可能希望在 View 中显示客户契约(Contract)的列表:
<% @customer.meters.each do |meter| %>
<% meter.contracts.each do |contract|
// display the contract
<% end %>
<% end %>
但是,您可以通过meters
关联将所有contracts
关联到customer
:
class Customer < ApplicationRecord
has_many :meters, -> { includes :contracts }
# ActiveRecord's shorthand declaration for associating
# records through a join table, in this case 'meters'
has_many :contracts, through: :meters
end
通过添加 has_many X, through: Y
,您是说客户记录通过连接表 与另一个表
.X
中的记录相关联>Y
有了这个,给定一个 客户
和现有的 meters
以及那些有现有 contracts
的meters,你将能够调用 @customer.contracts
和 ActiveRecord 将通过加入客户的 meters
和他们的 contracts
来获取 contracts
并返回 contracts
集合。
有关 Rails Has Many Through 协会的更多信息:http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association
关于ruby-on-rails - 事件记录 :has_many and includes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44270822/
我有一个具有以下模型的应用程序:- 类别- 子类别- 产品- 产品子类别 我的关系如下: Category has_many :subcategories Subcategory belongs_to
看来 rails 仍然不支持这种类型的关系并抛出 ActiveRecord::HasManyThroughAssociationPolymorphicThroughError 错误。 我该怎么做才能实
我想知道我可以在多大程度上使用 Rails 中的关联。考虑以下因素: class User :provider end class Provider :businesses belongs
我是 Rails 的新手。我想知道我是否需要将 add_index 添加到两个迁移? 我正在尝试定义用户和事件。每个用户可以有多个事件,每个事件可以有多个用户。所以我会做这样的事情: class Us
我有这个: class User :series end class Serial :serials end class Episode < ActiveRecord::Base belong
我一直致力于一个大量使用模型关联的项目,似乎我发现了 has_many 或 has_one through 功能与 :conditions 功能冲突的情况。简而言之,问题是 through 关联为 h
模拟以下情况的最佳方法是什么: Word belongs_to :wordable, :polymorphic => true Phrase has_many :words, :as => :
这似乎是一个典型的问题,但很难搜索。 我想选择用户通过 has_many 拥有的项目和用户通过 has_many through 关联的项目。 考虑以下模型: class User < ActiveR
首先对英语感到抱歉 所以我已经有了“用户 - 帖子”一对多关联,这意味着每个帖子只能有一个作者,现在我想在用户个人资料中添加“最喜欢的帖子”按钮,以及“添加到收藏夹”按钮每个帖子,所以问题是如何实现这
我有一个用户模型,允许用户关注其他用户。每个用户也有很多东西: class User has_many :following, :class_name => 'Followings', :fore
我正在使用 Rails 4.1 和 Ruby 2.1。 一个用户 has_many 辆车,和一辆车 has_many 约会。获取所有用户约会的最简单方法是什么? 理想情况下,我想做这样的事情:user
我想为 has_many/has_many 关系定义一个工厂(我想我做对了)但是我不知道如何为这些创建的工厂的每个对象定义属性。 这是主页,这是交易卡片的列表 我们假设下面的主页 View 是针对登录
我正在努力弄清楚如何实现这个计数。模型是用户、测试、等级 用户 has_many 测试,测试 has_many 成绩。 每个等级都有一个计算分数(strong_pass、pass、fail、stron
我有一个 has many through 关系来定义用户已回答的问题。 如何为用户创建的问题建立关系? 通常你会在用户和问题之间创建一个 has_many 和 belongs_to 关系,但是因为我
例如,在 class Student < ActiveRecord::Base has_many :awards end class Awards < ActiveRecord::Base b
我仍然在尝试如何处理在Ecto中创建/更新has_many, through:关联。我已经重新阅读了José's关于关联以及the docs的帖子,但我仍在努力。 我所拥有的是: 网站/模型/dish
# == Schema Information # # Table name: orders # # id :integer not null, pri
我正在尝试从 EventView.promoter_id = User.id 的用户获取事件,但它无法正常工作。这是我的设置(目前不正确): User has_many :events has_man
我有三门课 TeamMember , Service和ServiceTeamMember 我有 class Service has_many :service_team_members has
我需要列出所有用户及其销售的产品总量及其电子邮件和产品总量, class User :user).group('users.email').sum(:quantity) 关于mysql - 事件记录
我是一名优秀的程序员,十分优秀!