views:

100

answers:

1

I am trying to use the Whenever plugin for rails to perform a model process at certain times.

When I try to use the mail_out process in my User model, I get the following error. Can someone please point me in the right direction of what is going wrong?

/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1567:in `find_from_ids': Couldn't find User without an ID (ActiveRecord::RecordNotFound)
    from /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:616:in `find'
    from /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user.rb:9:in `mail_out'
    from (eval):1
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
    from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/runner:3

My schedule.rb is as follows:

   every 1.day, :at => '5:30 am' do
    runner "User.mail_out"
  end

My User model is:

class User < ActiveRecord::Base

  acts_as_authentic

  def self.mail_out

    weekday = Date.today.strftime('%A').downcase

    @users = find(:conditions => "#{weekday}sub = t")

    @users.each { |u| UserMailer.deliver_mail_out(u)}   


  end

end

My User_mailer is:

class UserMailer < ActionMailer::Base
    def mail_out(users)
    @recipients = { }
    users.each do |user|
      @recipients[user.email] = { :name => user.name }
    end


    from        "[email protected]"
    subject     "Check out the trailer of the day!"
    body        :user => user
  end

end

Migration:

  create_table "users", :force => true do |t|
    t.string   "email"
    t.date     "birthday"
    t.string   "gender"
    t.string   "zipcode"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "crypted_password"
    t.string   "password_salt"
    t.string   "persistence_token"
    t.string   "mondaysub",         :default => "f", :null => false
    t.string   "tuesdaysub",        :default => "f", :null => false
    t.string   "wednesdaysub",      :default => "f", :null => false
    t.string   "thursdaysub",       :default => "f", :null => false
    t.string   "fridaysub",         :default => "f", :null => false
    t.string   "saturdaysub",       :default => "f", :null => false
    t.string   "sundaysub",         :default => "f", :null => false
  end
+1  A: 

Change this:

find(:conditions => "#{weekday}sub = t")

to

find(:all, :conditions => "#{weekday}sub = t")
jonnii
/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: /var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log': SQLite3::SQLException: no such column: t: SELECT * FROM "users" WHERE (mondaysub = t) (ActiveRecord::StatementInvalid)Any idea on how to proceed?
Trevor Nederlof
What does your migration look like? Try `find(:all, :conditions => ["#{weekday}sub = ?", true])`
jonnii
I added my schema above. I tried the second suggestion and got:/var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:48: /var/lib/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:380:in `load_without_new_constant_marking': /home/tnederlof/Dropbox/Ruby/daily_trailer/app/models/user.rb:9: syntax error, unexpected tIDENTIFIER, expecting ')' (SyntaxError) @users = find(:all, :conditions => "#{weekday}sub = "t"")
Trevor Nederlof