循环 has_many 时如何从连接表获取信息:through
我有一个 has_many :through 关联,我正在迭代孩子们以整理一些信息。但我还想从循环中的连接表中获取一些信息。这是在需要帮助的部分周围有大量注释的方法。我是否能够以这样简单的方式从连接表中获取该会员编号?
class Customer < ActiveRecord::Base
has_many :customer_memberships
has_many :membership_programs, :through => :customer_memberships
def membership_info_to_json
info ={"benefits" => [], "omitted_stuff" => {}}
self.membership_programs.each do |membership|
##################################################################
#THIS INFO IS IN THE JOIN TABLE ##################################
info["membership_numbers"] << customer_membership.membership_number
##################################################################
#Omitted: the rest of the loop deals with membership.
end
info.to_json
end
I have a has_many :through association, and I am iterating over the children to put together some info. But I would also like to get some information out of the join table in the loop. Here is the method with blaring comments around the section that needs help. Am I able to get that membership number from the join table in a simple manner like this?
class Customer < ActiveRecord::Base
has_many :customer_memberships
has_many :membership_programs, :through => :customer_memberships
def membership_info_to_json
info ={"benefits" => [], "omitted_stuff" => {}}
self.membership_programs.each do |membership|
##################################################################
#THIS INFO IS IN THE JOIN TABLE ##################################
info["membership_numbers"] << customer_membership.membership_number
##################################################################
#Omitted: the rest of the loop deals with membership.
end
info.to_json
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的尝试有些让我困惑。我不太确定它是什么,但除非我弄错了,否则会员计划也将拥有许多客户会员资格。如果是这种情况,那么您必须为该会员资格收集每一个。
我希望这有帮助。
Something about your attempt is confusing me. I'm not exactly sure what it is but unless I'm mistaken, a membership_program is going to also have many customer_memberships. If that's the case then you'll have to collect each for that membership.
I hope that helps.