Create Rails model with argument of associated model?

Posted by Kyle Carlson on Stack Overflow See other posts from Stack Overflow or by Kyle Carlson
Published on 2012-11-24T00:14:45Z Indexed on 2012/11/24 5:05 UTC
Read the original article Hit count: 112

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!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby