Can't seem to create an association

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:

CREATE TABLE users (
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
down)

class User < ActiveRecord::Base
has_one :provider

def invoices_raised
provider.get_invoices
end

end

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

def get_invoices
invoices.find(:all)
end
end

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

end

end

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs