Реализую реферальную систему рельсов 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
my 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
Теперь система рефералов работает, я могу создавать членов для их реферера
Задача состоит в том, чтобы просмотреть 1-го поколения до 8-го поколения.Я получаю эту ошибку:
undefined method `referrals' for #<User::ActiveRecord_Associations_CollectionProxy:0x00007f48e4c2d2e0>
Я в состоянии рефералов для первого поколения, но со 2-го и выше он не работает.
Любая помощь, пожалуйста ... ???Вот мой контроллер панели приборов
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