我正在实现一个引用系统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
答案 0 :(得分:0)
错误来自仪表板控制器这一行
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
答案 1 :(得分:0)
现在我开始工作了。
完全用户对象仅在该级别的集合@user.referrals
的首次调用之后可用,之前的调用无法获得此用户引用的用户组的uniq关联。所以我做了这样的事情:我使用数据库中的引用代码来搜索并返回正确的引用。即使性能下降,它也可以一直工作到第八代。
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