Unable to create associations

I have two tables, user & provider and I'm struggling to set up the
association between them.

A simplified table structure for the two is:

    id integer not null,
    login string not null,
    password string not null

CREATE TABLE providers (
    id integer not null,
    user_id integer not null,

and I've used the generater to create two modules, as follows (cut

class User < ActiveRecord::Base
  has_one :provider

  def invoices_raised



class Provider < ActiveRecord::Base
  has_many :invoices
  belongs_to :user

  def get_invoices

I have a controller that calls invoices_raised :

class AccountController < ApplicationController
  model :user

  before_filter :get_invoices

  def get_invoices
    @user = User.find(@session['user'].id)

    @invoices_raised = @user.invoices_raised



but inside the invoices_raised method I get the following error:

You have a nil object when you didn't expect it!
The error occured while evaluating nil.get_invoices

I thought that using belongs_to & has_one was supposed to implicitly
create a provider instance for me?

Bryan Evans wrote:

User.has_one :provider will create an accessor on your User class that
retrieves the Provider associated with a given user u (i.e. select *
from providers where providers.user_id=u.id). There's not enough info in
your post to determine why this method is returning a nil for your User
instance; I would need to see the object that you are calling
get_invoices on and how it is created.

get_invoices is called :

before_filter :get_invoices, :only => [:my_account]

Thanks for the reply Bryan, I know my understanding of asscociations is
poor, and I have read the docs again now (and tbh, I think I need to
again) but at that time in the morning, it seemed like the best thing
to do .. :wink: