Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Custom current_year module accessible in view but not model


  • Please log in to reply
1 reply to this topic

#1 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 20 February 2014 - 06:40 PM

I'm buliding a website to manage a very complex fantasy football league I'm in and I've created a custom module for calculating the current_year based on the league's season (August 1 through July 31 every year).

 

Because this logic is necessary in a number of models, I created a ModifiedCurrentYear module that I include where needed so I can access the current_year value.

 

In every other module, it's working fine, but in my SuperBowl model (one of the league awards is $5 for picking the right Super Bowl winner), I keep getting "undefined local variable or method `current_year`"

 

lib/modified_current_year.rb

module ModifiedCurrentYear
  def current_year
    # The new seasons starts on 8/1 so technically, it's the previous year through 7/31
    current_date = Time.now
    if current_date.month < 8
      current_date.year - 1
    else
      current_date.year
    end
  end
end

And then my SuperBowl model:

 

models/super_bowl.rb

class SuperBowl < ActiveRecord::Base
  include ModifiedCurrentYear

  attr_accessible :dopr_winner_id, :nfl_team1_final_score, :nfl_team1_id, :nfl_team2_final_score, :nfl_team2_id, :nfl_winner_id, :tie_breaker, :year

  belongs_to :nfl_team
  belongs_to :team
  has_many :super_bowl_picks

  scope :current_super_bowl, lambda { where("year = ?", current_year) }

end

And, like I said: I'm using this module in 4 or 5 other models with no issue. What's REALLY infuriating is that I can call <%= current_year %> in the super_bowls#index view and it returns "2013" as expected. And if I put @current_sb = SuperBowl.find_by_year(current_year) in the controller, it returns the object I want no problem.

 

But any time I try to use current_year in the model, I get the aforementioned error. I just don't get it. Why does this module work in some models and not others? And I've tried going the route of using a method instead of a scope and it makes no difference.

 

I've tried the method the following ways:

def current_super_bowl
    SuperBowl.where("year = ?", current_year)
end

============

def current_super_bowl
    self.where("year = ?", current_year)
end

============

def current_super_bowl
   sb = SuperBowl.all
   sb.each do |s|
        if s.year == current_year
            s
        end
    end
end

Along with the same methods but with self. in front of the method name. Same "undefined local variable or method" error.

 

I've also tried copying the current_year method from the module into the SuperBowl model file and nothing works.

 

Can anyone offer some insight into what I'm doing wrong here because I have a feeling it's something stupid that I'm just overlooking and I'm getting to the point of throwing my laptop.



#2 klyrish

klyrish

    Signalman

  • Members
  • 10 posts
  • LocationDenver, CO

Posted 20 February 2014 - 10:02 PM

I got it working by copying the current_year method from the module into the SuperBowl.rb model file and adding self. in front of it.

 

I don't understand why this was necessary in order to be able to use the method, though. Any insight?






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users