对于#<User :: ActiveRecord_Associations_CollectionProxy,未定义的方法`referrals':

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (60)

我正在实施一个推荐系统rails 5.1。我想推荐第8代。我有一个联盟类如下:

class Affiliation < ApplicationRecord
  belongs_to :affiliate, :class_name => "User", :foreign_key => "affiliate_id"
  belongs_to :referred, :class_name => "User", :foreign_key => "referred_id"
     validates_presence_of :affiliate, :referred
     validates_uniqueness_of :referred_id
       private
        def validate
          errors.add_to_base("Affiliate and Referrer can't be the same 
        user.") if affiliate and (affiliate == referred)
        end
       end

我的User.rb

class User < ApplicationRecord
 has_many :affiliations, :foreign_key => "affiliate_id", :dependent => :destroy
   has_many :referrals, :through => :affiliations, :source => :referred
  # map.affiliate_referral 'a/:referrer_code', :controller => 
 'referrals', :action => 'new'
  def affiliate_link
    "https://localhost:3000/members/new/?referrer_code=#
    {self.affiliate_code}"
 end

现在推荐系统正在工作我可以为他们的推荐人创建成员

挑战是如何看第一代到第八代。我得到这个错误:

undefined method `referrals' for #<User::ActiveRecord_Associations_CollectionProxy:0x00007f48e4c2d2e0>

我可以推荐第一代,但从第二代推出它不起作用。

请帮忙...... ??? 这是我的仪表板控制器

class DashboardController < ApplicationController
         def dashboard
                 # storage for referrals and affilations through eight 
         generations

           #generate first generation referrals
             def flatten_scopes(scopes)
            scopes.inject([]) { |a,r| r.is_a?(Array) ? a += flatten_scopes(r) : a.push(r) }
   end

       @firstgeners = referral_gen(current_user)
       @secondgeners ||= []
       @thirdgeners ||= []
       @fourthgeners ||= []
      @fifthgeners ||= []
      @sixthgeners ||= []
     @seventhgeners ||= []
     @eightgeners ||= []
            #loop first generation referrals to get the affiliates to 
         form our second 
        generation affiliations 
        #@firstgeners = @firstgeners1.flatten

        #@secondgeners1 = []
               @firstgeners = flatten_scopes(@firstgeners)
             if  @firstgeners.any?
            @firstgeners.each do |u| 
        if u.referrals.any?

           @secondgeners << u.referrals
           end
       end
        end

      #@secondgeners = @secondgeners1.flatten
      #from second generation referrals generate the affiliations 
      # @thirdgeners1 = []
      @secondgeners = flatten_scopes(@secondgeners)
      if @secondgeners.any?
      @secondgeners.each do |u| 
      if u.referrals.any?
     @thirdgeners << u.referrals
       end
     end
    end
    #@thirdgeners = @thirdgeners1.flatten

     @thirdgeners = flatten_scopes(@thirdgeners)
     #from third generation referrals generate the affiliations
    if @thirdgeners.any?
      @thirdgeners.each do |u| 
     if u.referrals.any?
      @fourthgeners << u.referrals
    end
    end
    end 
    #@fourthgeners = @fourthgeners1.flatten



      #from third generation referrals generate the affiliations
        @fourthgeners = flatten_scopes(@fourthgeners)
        if @fourthgeners.any?
       @fourthgeners.each do |u| 
       if u.referrals.any?
       @fifthgeners << u.referrals
       end
       end
       end 
    # @fifthgeners = @fifthgeners1.flatten

     @fifthgeners = flatten_scopes(@fifthgeners)
     if @fifthgeners.any?
        @fifthgeners.each do |u| 
         if u.referrals.any?
           @sixthgeners << u.referrals
          end
          end
          end

       #  @sixthgeners = @sixthgeners1.flatten 
        @sixthgeners = flatten_scopes(@sixthgeners)
         if @sixthgeners.any?
          @sixthgeners.each do |u| 
          if u.referrals.any?
          @seventhgeners << u.referrals
          end
         end
         end
         #@seventhgeners = @seventhgeners.flatten

        @secondgeners = flatten_scopes(@seventhgeners)
         if @seventhgeners.any?
           @seventhgeners.each do |u| 
           if u.referrals.any?
            @eightgeners << u.referrals
           end
          end
           end 

         #@eightgeners = @eightgeners1.flatten
         end

         #generate all affiliations  of current_user

               def referral_gen(user)
                 referrals = user.referrals
                 return referrals
               end
提问于
用户回答回答于

错误来自仪表板控制器这一行

                  if u.referrals.any?
                  @thirdgeners << u.referrals

这条线位于下面的区块内..一旦我的第一代转介推荐给其他人。发生错误。如果他们没有它运作良好。只是调用我的推荐它很有效,因为我有活跃的记录关联。但我tot current_user.referrals会给我一组用户。我可以遍历它们并获得他们的推荐以及user.referrals并重复直到8次。但不是这样的。

 @secondgeners = flatten_scopes(@secondgeners)
       if @secondgeners.any?
           @secondgeners.each do |u| 
           if u.referrals.any?
              @thirdgeners << u.referrals
         end
         end
        end

扫码关注云+社区

领取腾讯云代金券