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 9054789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:46:31+00:00 2026-06-16T13:46:31+00:00

I am trying to update a column in my database if a book was

  • 0

I am trying to update a column in my database if a book was created more than 25 seconds ago (when deploying, it will be 7 days ago, but I can’t wait that long :)).

Models:

class Book < ActiveRecord::Base
  attr_accessible :author, :date, :description, :order, :title, :user_id, :author, :status, :queued
  belongs_to :user
end

class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :remember_me, :user_id, :name, :bio
  has_many :books
end

Controller:

class UsersController < ApplicationController

def show
  @user = User.find(params[:id])

  @book = Book.new

  @books = Book.select("DISTINCT name, id") # Not used right now

  @sequence = @user.books

  @sequence.each do |book|
    book.created_at >= 25.seconds.ago ? book.queued = false : nil
  end
end

User show view:

<% @sequence.order("created_at DESC").where(:queued => false).each do |book| %>

Am I even close to getting this to work? What am I doing wrong? As you can see, I want to change the “queued” attribute to false after 25 seconds…

Edit

I’ve been told I need to use something like Heroku’s Scheduler for this to work. Is there no way for this to update the database without something like that?

  • 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-16T13:46:32+00:00Added an answer on June 16, 2026 at 1:46 pm

    You could take a different approach. Instead of having a field to know if it’ queued use a scope (a query for queued books)

    class Book < ActiveRecord::Base
      scope :queued, where('created_at <= ?', 25.seconds.ago)
      scope :not_queued, where('created_at > ?', 25.seconds.ago)
    end
    

    So now in your controller you can do something like:

    class UsersController < ApplicationController
    
      def show
        @user = User.find(params[:id])
    
        @book = Book.new
    
        @not_queued_books = @user.books.not_queued
      end
    end
    

    Does it resolve your problem or you really need to have that column? Because with a scope it totally works and it’s away more flexible and easy to implement!


    If you want to see the ones not_queue by default Book.all and have a scope to see the queued ones here is an example

    class Book < ActiveRecord::Base
      scope :queued, where('created_at <= ?', 25.seconds.ago)
      scope :not_queued, unscoped.where('created_at > ?', 25.seconds.ago)
      scope :date_desc, order("created_at DESC")
      default_scope not_queued
    end
    
    
    class UsersController < ApplicationController
    
      def show
        @user = User.find(params[:id])
    
        @book = Book.new
      end
    end
    

    So now in your view just do this:

    <% @user.books.date_desc.each do |book| %>
    

    Notice now the default scope is books not queued and I moved the ordering also to a scope. Of course you can do this books.date_desc in your controller wich should preferable.


    So as you said in the comment you have the issue of evaluation in scopes. Well, this is because the scope is “cached” when you start the app, so 25.seconds.ago will be relative to the time you started the app instead of the 25 seconds ago from now as you want to. There are plenty resources explaining this, and you should go check them if you don’t understand what I’m saying. For example in this railscast http://railscasts.com/episodes/202-active-record-queries-in-rails-3

    So what you have to do? You have to wrap your scopes with a lambda so it will be evaluated every time you use the scope instead of evaluating when you start the app.

    Like this:

      scope :queued, lambda { where('created_at <= ?', 25.seconds.ago) }
      scope :not_queued, lambda { unscoped.where('created_at > ?', 25.seconds.ago) }
      scope :date_desc, order("created_at DESC")
      default_scope not_queued
    

    Also instead of lambda { ... } you can use the new 1.9 shorthand -> { ... } instead

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to update an old Access database using SQL scripts, but I can't
I'm trying to update the records in an Access database where the column Group
I'm trying to update a column to a max value, but only when grouped
I'm trying to UPDATE a column for a user, so that they can only
SEE: Update timestamp column in Application or Database? I'm trying to model something similar
Im trying to update the html column in every row in my database when
I am trying to update a single column in a SQLite database table in
I am trying to update my date column in oracle using nhibernate but I
I'm trying to update a Postgres database to set a boolean but I'm getting
I'm trying to use a trigger to update a column on my database when

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.