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

  • Home
  • SEARCH
  • 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 8886653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:36:34+00:00 2026-06-14T21:36:34+00:00

I have two models, User and PushupReminder , and a method create_a_reminder in my

  • 0

I have two models, User and PushupReminder, and a method create_a_reminder in my PushupReminder controller (is that the best place to put it?) that I want to have create a new instance of a PushupReminder for a given user when I pass it a user ID. I have the association via the user_id column working correctly in my PushupReminder table and I’ve tested that I can both create reminders & send the reminder email correctly via the Rails console.

Here is a snippet of the model code:

class User < ActiveRecord::Base
    has_many :pushup_reminders
end

class PushupReminder < ActiveRecord::Base
    belongs_to :user
end

And the create_a_reminder method:

def create_a_reminder(user)
    @user = User.find(user)

    @reminder = PushupReminder.create(:user_id => @user.id, :completed => false, :num_pushups => @user.pushups_per_reminder, :when_sent => Time.now)

    PushupReminderMailer.reminder_email(@user).deliver
end

I’m at a loss for how to run that create_a_reminder method in my code for a given user (eventually will be in a cron job for all my users). If someone could help me get my thinking on the right track, I’d really appreciate it.

Thanks!

  • 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-14T21:36:36+00:00Added an answer on June 14, 2026 at 9:36 pm

    Edit: I’ve posted a sample Rails app here demonstrating the stuff I’m talking about in my answer. I’ve also posted a new commit, complete with comments that demonstrates how to handle pushup reminders when they’re also available in a non-nested fashion.

    Paul’s on the right track, for sure. You’ll want this create functionality in two places, the second being important if you want to run this as a cron job.

    1. In your PushupRemindersController, as a nested resource for a User; for the sake of creating pushup reminders via the web.
    2. In a rake task, which will be run as a cron job.

    Most of the code you need is already provided for you by Rails, and most of it you’ve already got set in your ActiveRecord associations. For #1, in routes.rb, setup nested routes…

    # Creates routes like...
    #   /users/<user_id>/pushup_reminders
    #   /users/<user_id>/pushup_reminders/new
    #   /users/<user_id>/pushup_reminders/<id>
    resources :users do
      resources :pushup_reminders
    end
    

    And your PushupRemindersController should look something like…

    class PushupRemindersController < ApplicationController
      before_filter :get_user
    
      # Most of this you'll already have.
    
      def index
        @pushup_reminders = @user.pushup_reminders
        respond_with @pushup_reminders
      end
    
      # This is the important one.
      def create
        attrs = {
          :completed => false,
          :num_pushups => @user.pushups_per_reminder,
          :when_sent => Time.now
        }
        @pushup_reminder = @user.pushup_reminders.create(attrs)
        respond_with @pushup_reminder
      end
    
      # This will handle getting the user from the params, thanks to the `before_filter`.
      def get_user
        @user = User.find(params[:user_id])
      end
    end
    

    Of course, you’ll have a new action that will present a web form to a user, etc. etc.

    For the second use case, the cron task, set it up as a Rake task in your lib/tasks directory of your project. This gives you free reign to setup an action that gets hit whenever you need, via a cron task. You’ll have full access to all your Rails models and so forth, just like a controller action. The real trick is this: if you’ve got crazy custom logic for setting up reminders, move it to an action in the PushupReminder model. That way you can fire off a creation method from a rake task, and one from the controller, and you don’t have to repeat writing any of your creation logic. Remember, don’t repeat yourself (DRY)!

    One gem I’ve found quite useful in setting up cron tasks is the whenever gem. Write your site-specific cron jobs in Ruby, and get the exact output of what you’d need to paste into a cron tab (and if you’re deploying via Capistrano, total hands-off management of cron jobs)!

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

Sidebar

Related Questions

I have two models User and Category that have a HABTM association. I would
I have two models -- User and Entry -- that are related through a
I have two models User and Admin(with RailsAdmin) that use Devise. I sign in
I have two models: User MentoringRelationship MentoringRelationship is a join model that has a
I have two models: User has_many :prices Price belongs_to :users I want to show
I have two models user and profile. I want to save the username and
I have two models: User (pre-defined by Django) and UserProfile that are connected through
I have created two models :User and UserProfile , Now I want display in
I have two models named message.php and user.php In message.php, I have following method
I have two models: User and Car with the following associations: User has_many Car

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.