简体   繁体   English

#的未定义方法“引荐”

[英]undefined method `referrals' for #<User::ActiveRecord_Associations_CollectionProxy:

I am implementing a referral system rails 5.1. 我正在实现引荐系统Rails 5.1。 I want to referrals up to 8th generation. 我想推荐第8代以下的人。 I have an affiliation class as below: 我有一个隶属关系类,如下所示:

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

my User.rb 我的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

Now the referral system is working I can create members to their referrer 现在推荐系统正在运行,我可以为其推荐人创建成员

the challenge is how to view 1st generation down to 8th generation. 面临的挑战是如何看待第一代到第八代。 Im getting this error: 我收到此错误:

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

I am able to referrals for first generation but from 2nd upward its not working . 我可以推荐第一代,但是从第二代开始它不起作用。

Any help please...??? 任何帮助请... ??? Here is my dashboard controller 这是我的仪表板控制器

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

Error is from the Dashboard Controller this line 错误是来自仪表板控制器的这一行

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

This line is inside the block below.. once my 1st generation referrals have referred other people. 这行是在下面的方框内..一旦我的第一代推荐人推荐了其他人。 The error occurs. 发生错误。 If they haven't it works well. 如果没有,那就行得通。 just calling my referrals it works well since I have the active record association as that. 只需致电我的推荐人就可以了,因为我拥有活动记录关联。 But I tot current_user.referrals would give me arrays of users. 但是我给current_user.referrals会给我数组用户。 And I can loop through them and get their referrals as well like user.referrals and repeat until 8 times. 而且我可以遍历它们并获得他们的引荐以及user.referrals并重复直到8次。 But not working like that. 但是不能那样工作。

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

now i get it to work. 现在我开始工作了。 full user object is only available at the first call of the collection @user.referrals after this level previous calls couldnot get uniq associations for the group of users referred by this user. 完整用户对象仅在集合@user.referrals的第一个调用中可用。在此级别之前,先前的调用无法获得此用户引用的用户组的uniq关联。 so I did something like this: I used the referral code from the database to search and return the right referrals. 所以我做了这样的事情:我使用数据库中的引用代码来搜索并返回正确的引用。 it works upwards till 8th generation even if it is slow on performance. 即使性能下降,它也可以一直工作到第八代。

if @firstgeners.any?
  @firstgeners.each do |u|
  @users.each do |e|
    if e.referrer_code == u.affiliate_code
      @secondgeners.push(e)
    end
  end
end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复“未定义的方法‘用户’(ActiveRecord_Associations_CollectionProxy) - How to fix "undefined method `user' (ActiveRecord_Associations_CollectionProxy) #的未定义方法`count =&#39; - undefined method `count=' for #<User::ActiveRecord_Associations_CollectionProxy Rails“ActiveRecord_Associations_CollectionProxy的未定义方法” - Rails “undefined method for ActiveRecord_Associations_CollectionProxy” ActiveRecord_Associations_CollectionProxy的未定义方法[rails] - undefined method for ActiveRecord_Associations_CollectionProxy [rails] 创建提要=&gt; NoMethodError:ActiveRecord_Associations_CollectionProxy的未定义方法 - Creating a feed => NoMethodError: undefined method for ActiveRecord_Associations_CollectionProxy ActiveRecord_Associations_CollectionProxy的未定义方法&#39;id_tag&#39; - undefined method 'id_tag' for ActiveRecord_Associations_CollectionProxy ActiveRecord_Associations_CollectionProxy上的重写方法 - Override method on ActiveRecord_Associations_CollectionProxy ActiveRecord_Associations_CollectionProxy问题 - ActiveRecord_Associations_CollectionProxy issue Rails,ActiveRecord_Associations_CollectionProxy调用ActiveRecord方法的方式 - Rails, The way for ActiveRecord_Associations_CollectionProxy to call method of ActiveRecord &gt; Client :: ActiveRecord_Associations_CollectionProxy的未定义方法&#39;to_key&#39;仅在编辑期间 - Undefined method 'to_key' for > Client::ActiveRecord_Associations_CollectionProxy during edit only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM