循环 has_many 时如何从连接表获取信息:through

发布于 2025-01-04 07:01:01 字数 789 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声无音无过去 2025-01-11 07:01:01

你的尝试有些让我困惑。我不太确定它是什么,但除非我弄错了,否则会员计划也将拥有许多客户会员资格。如果是这种情况,那么您必须为该会员资格收集每一个。

def membership_info_to_json
  info ={"membership_numbers"=>[], "benefits" => [], "omitted_stuff" => {}}
  self.membership_programs.each do |membership|
    info["membership_numbers"] << membership.customer_memberships.collect{|cm| cm.membership_number} 
  end
  info["membership_numbers"].flatten!
  info.to_json
end 

我希望这有帮助。

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.

def membership_info_to_json
  info ={"membership_numbers"=>[], "benefits" => [], "omitted_stuff" => {}}
  self.membership_programs.each do |membership|
    info["membership_numbers"] << membership.customer_memberships.collect{|cm| cm.membership_number} 
  end
  info["membership_numbers"].flatten!
  info.to_json
end 

I hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文