[Rails] actionmailer issues

Nolan Darilek nolan at thewordnerd.info
Thu Jan 27 21:34:48 GMT 2005


On Wed, Jan 26, 2005 at 06:06:05PM -0700, Jeffrey Moss wrote:
> Nolan,
> 
> Why do you use a confirmation_id field as an attr_accessor? Why not juse 
> use an association? I can't see an obvious problem with your code, but I do 
> the exact same thing, here's how I do it:
> 

I was hesitant because I wasn't sure whether it'd be worth storing a
confirmation association with each user, especially as it'd be used
only once, but I'm trying to tweak things to use associations
now. Here is what I have now:

class User < ActiveRecord::Base
  has_one :confirmation
. . .
end

class Confirmation < ActiveRecord::Base
  belongs_to :user
end

My schema:
CREATE TABLE confirmations(id integer not null primary key, user_id integer not null);
CREATE TABLE users ( id integer NOT NULL PRIMARY KEY, login varchar default NULL, password varchar default NULL, email varchar not null, valid boolean default false, confirmation_id integer default 0);

And my AccountController#confirm_email:
  def confirm_email
    @user = User.find_first("id = #{@params["id"]}")
    confirmation = @user.confirmation.build(:id => rand(Time.now)) # line 48
@user.confirmation = confirmation
    Accounts.deliver_signup_email_confirm(@user)
  end

My user is created, but its confirmation_id is set to 0, and I receive
the following error in my logs:

NoMethodError (undefined method `build' for nil:NilClass):
    /app/controllers/account_controller.rb:48:in `confirm_email'
. . .

I've also tried creating a Confirmation, assigning its values directly
then saving it, but I get errors claiming that confirmation.user_id
can't be NULL (this was when I allowed for NULLs in the schema) so it
seems that something is trashing that value somewhere and I don't know
where.

Any ideas about what might be wrong? Furthermore, how can I debug
this? I'm trying .inspect, but it doesn't seem to appear anywhere in
my logs.


More information about the Rails mailing list