Rails 4:辅助方法未检索关联记录

Rails 4: Helper method not retrieving associated records

我有一个自定义帮助程序方法,其唯一目的是计算将显示在视图中的关联记录的数据。在 rails 控制台中,我可以添加 Campaign.find(1).plan.subscribers 并检索订阅者列表,但我无法在我的自定义帮助程序方法中执行此操作。

这是辅助方法.. (注意:您可以看到我尝试打印的地方 campaign.plan.subscribers 以查看它是否在循环之前注册...不是)

helpers/application_helper.rb

module ApplicationHelper

    def current_recipients(campaign)
        @target_program = campaign.program.name
        @recipients = 0

        #p campaign.plan.subscribers

        campaign.plan.subscribers.each do |s|
            case @target_program
                when "Caspian Star" && s.star?
                    @recipients += 1
                when "STIX" && s.stix?
                    @recipients += 1
                when "PPCI" && s.ppci?
                    @recipients += 1
                else
                    @recipients += 0
                end
        end
        @recipients
    end
end

我的规格..

spec/helpers.application_helper_spec.rb

require "rails_helper"

RSpec.describe ApplicationHelper, :type => :helper do

    describe "Counting campaign recipients" do
        subject(:campaign) { create(:campaign, plan: plan, program: program) }
        let(:program) { create(:program) }
        let(:plan) { create(:plan) }

        it "finds subscribers under campaign plan and program" do
            expect(helper.current_recipients(campaign)).to eq 3
        end
    end
end

失败

  1) ApplicationHelper Counting campaign recipients finds subscribers under campaign plan and program
     Failure/Error: expect(helper.current_recipients(campaign)).to eq 3

       expected: 3
            got: 0

       (compared using ==)

人际关系..

app/models/..

Class Campaign < ActiveRecord::Base
    belongs_to :program
    belongs_to :plan
end

Class Plan < ActiveRecord::Base
    has_many :campaigns
    has_many :subscribers, through: :plannables
    has_many :plannables
end

Class Program < ActiveRecord::Base
    has_many :campaigns
end

class Subscriber < ActiveRecord::Base
    has_many :plans, through: :plannables
    has_many :plannables
end

class Plannable < ActiveRecord::Base
    belongs_to :plan
    belongs_to :provider
end

编辑

这是按要求添加的工厂。

FactoryGirl.define do

    factory :campaign do |x|
        x.sequence(:name) { |y| "Q6 201#{y}" }
        x.sequence(:comment) { |y| "JIRA OI-6#{y}" }
        channels ["Folder", "Fax"]
    end

    factory :program do
        name "Caspian Star"
    end

    factory :plan do
        name "Third Storm Connect"
    end
end

问题最终出现在 case 语句中,该语句没有抛出任何明显的错误。它对 when 条件下的 && 反应不佳,所以我将它们放在下面,现在似乎可以正常工作了。这是代码现在的样子..

module ApplicationHelper

    def current_recipients(campaign)
        @target_program = campaign.program.name
        @recipients = 0

        campaign.plan.subscribers.each do |s|
            case @target_program
                when "Caspian Star"
                    s.star?
                    @recipients += 1
                when "STIX"
                    s.stix?
                    @recipients += 1
                when "PPCI"
                    s.ppci?
                    @recipients += 1
                else
                    @recipients += 0
                end
        end
        @recipients
    end
end

根据@steveklein 在聊天会话期间提出的建议,我能够通过以这种方式将我的对象关联链接在一起来让我的测试通过..

describe "Counting campaign recipients" do
    it "finds subscribers under chosen campaign plan and program" do
        campaign = create(:campaign)
        plan = create(:plan)
        campaign.plan = plan
        campaign.program = create(:program)

    3.times do
        subscriber = create(:subscriber, star: true)
        subscriber.plans << plan
    end

        expect(helper.current_recipients(campaign)).to eq 3
    end
end