Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9084073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:55:09+00:00 2026-06-16T20:55:09+00:00

I would like this before filter to run every time the page is loaded

  • 0

I would like this before filter to run every time the page is loaded (for now) to check if an item is over 7 days old or not and if so, run some actions on it to update its attributes.

I have before_filter :update_it in the application controller. update_it is defined below that in the same controller as:

def update_it
  @books = Book.all
  @books.each do |book|
    book.update_queue
  end
end

Then update_queue is defined in the book model. Here’s everything in the model that pertains to this:

scope :my_books, lambda {|user_id|
    {:conditions => {:user_id => user_id}}  
  }

  scope :reading_books, lambda {
    {:conditions => {:reading => 1}}
  }

  scope :latest_first, lambda {
    {:order => "created_at DESC"}
  }


  def move_from_queue_to_reading
    self.update_attributes(:queued => false, :reading => 1);
  end

  def move_from_reading_to_list
    self.update_attributes(:reading => 0);
  end

  def update_queue
    days_gone = (Date.today - Date.parse(Book.where(:reading => 1).last.created_at.to_s)).to_i

    # If been 7 days since last 'currently reading' book created
    if days_gone >= 7

        # If there's a queued book, move it to 'currently reading'
        if Book.my_books(user_id).where(:queued => true)
            new_book = Book.my_books(user_id).latest_first.where(:queued => true).last
            new_book.move_from_queue_to_reading
            currently_reading = Book.my_books(user_id).reading_books.last
            currently_reading.move_from_reading_to_list

        # Otherwise, create a new one
        else
            Book.my_books(user_id).create(:title => "Sample book", :reading => 1)

        end
    end
  end

My relationship is that a book belongs_to a user and a user has_many books. I’m showing these books in the view through the user show view, not that it matters though.

So the errors I keep getting are that move_from_queue_to_reading and move_from_reading_to_list are undefined methods. How can this be? I’m clearly defining them and then calling them below. I really am at a loss and would greatly appreciate some insight into what I’m doing wrong. I’m a beginner here, so any structured criticism would be great 🙂

EDIT

The exact error message I get and stack trace is as follows:

NoMethodError in UsersController#show
undefined method `move_from_queue_to_reading' for nil:NilClass

app/models/book.rb:41:in `update_queue'
app/controllers/application_controller.rb:22:in `block in update_it'
app/controllers/application_controller.rb:21:in `each'
app/controllers/application_controller.rb:21:in `update_it'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:55:10+00:00Added an answer on June 16, 2026 at 8:55 pm

    I suspect that the collection returned is an empty array (which is still ‘truthy’ when tested). So calling .last is returning nil to the new_book and currently_reading local variables. Try changing:

    if Book.my_books(user_id).where(:queued => true)
    

    to:

    if Book.my_books(user_id).where(:queued => true).exists?
    

    Additionally, you are modifying the scope when finding currently_reading. This can potentially cause the query to again return no results. Change:

    currently_reading.move_from_reading_to_list
    

    to:

    currently_reading.move_from_reading_to_list if currently_reading
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In the Jquery example below, I would like to expand $(this) before it is
I would like to create kind of a before filter which allows me to
I would like to set a before filter in a rails app as follows:
I would like to strtotime dateformat like this - Fri Jun 15 05:38:11 +1000
I would like to do something like this: abstract class Foo { public function
I would like to do something like this in HTML5 where I have something
I would like to recreate something like this if ( arg1 || arg2 ||
I would like to have declaration like this: void Date::get_days_name(const Date& = this) which
I would like to do something like this i.e., use wild card characters in
I would like do some thing like this: SELECT sum(quantity) FROM orders WHERE order_code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.