加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – ruby?? on rails undefined方法的数组

发布时间:2020-12-17 03:33:35 所属栏目:百科 来源:网络整理
导读:我有一个拥有许多手机的用户 我有一部手机,里面有很多通话摘要 因此我的用户有很多通话摘要 现在到我的代码: class User ActiveRecord::Base has_many :phones has_many :call_summaries,:through = :phonesend class Phone ActiveRecord::Base belongs_to
我有一个拥有许多手机的用户
我有一部手机,里面有很多通话摘要
因此我的用户有很多通话摘要

现在到我的代码:

class User < ActiveRecord::Base
    has_many :phones
    has_many :call_summaries,:through => :phones
end  

class Phone < ActiveRecord::Base
    belongs_to :user
    has_many :call_summaries
end

class CallSummary < ActiveRecord::Base
    belongs_to :phones
end

我想生成一个报告,显示属于该特定用户的电话的所有呼叫摘要.我进入控制器,这是我的代码:

def index
  @phones = Phone.find(:all,:conditions => ["user_id = ?",@current_user.id])
  @call_summaries = @phones.call_summaries.find(:all)
end

但是这会返回此错误:

undefined method `call_summaries’ for #Array:0x476d2d0

任何帮助将非常感谢.

解决方法

如果您设置了has_many:through关系,那么您应该能够:

@call_summaries = @current_user.call_summaries

您的方法的问题是您在@phones集合上调用call_summaries,而不是在单个电话实例上调用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读