# Unable to create associations

**URL:** <https://www.ruby-forum.com/t/unable-to-create-associations/75320>\
**Category:** Rails\
**Created:** [November 9, 2006, 3:01am UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320 "2006-11-09T03:01:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [November 9, 2006, 3:01am UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320/1 "2006-11-09T03:01:20Z")

</div>

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?

–  
-S

Sports Photography in South Yorkshire & Derbyshire  
[http://www.stuartgrimshaw.co.uk](http://www.stuartgrimshaw.co.uk)

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [November 9, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320/2 "2006-11-09T17:49:19Z")

</div>

Stuart G. wrote:

> class User \< ActiveRecord::Base  
> has\_one :provider
> 
> def invoices\_raised  
> provider.get\_invoices  
> end
> 
> …
> 
> end

What if you change “provider.get\_invoices” to  
“self.provider.get\_invoices”?

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [November 9, 2006, 4:52am UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320/3 "2006-11-09T04:52:14Z")

</div>

Stuart G. wrote:

> I have two tables, user & provider and I’m struggling to set up the  
> association between them.  
> {…}  
> class User \< ActiveRecord::Base  
> has\_one :provider
> 
> def invoices\_raised  
> provider.get\_invoices  
> 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?
> 
> –  
> -S

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.

I would recommend brushing up on what the associations methods do  
(belongs\_to, has\_many, etc), and you will likely discover the source of  
the error

For example…

> class Provider \< ActiveRecord::Base  
> has\_many :invoices  
> belongs\_to :user
> 
> def get\_invoices  
> invoices.find(:all)  
> end  
> end

Calling has\_many :invoices actually creates an instance method  
“invoices” in Provider that returns all Invoice objects associated with  
your Provider instance. I’m guessing that this is what you hoped to  
achieve with get\_invoices.

I’m not sure what you’re expecting from your call “invoices.find(:all)”.  
The invoices method returns all Invoice objects associated with your  
Provider instance, and “find” is actually an ActiveRecord::Base class  
method which will raise an error when you call it on a collection of  
Invoices, which is what you’re doing here. find(:all) retrieves _all_  
records from a particular table, and I’m guessing that’s not what you’re  
trying to do here. If it is, you’ll want to call Invoice.find(:all)

I would recommend checking out the Rails documentation for associations:  
[http://api.rubyonrails.org/](http://api.rubyonrails.org/)  
(click on ActiveRecord::Associations::ClassMethods in the Class frame)

Hope this helps

- Bryan

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [November 10, 2006, 12:02am UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320/4 "2006-11-10T00:02:53Z")

</div>

Bryan E. 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 … 😉

-S

---

<div class="post-metadata">

**Author:** ![stubbsuk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e56c9b/32.png) [@stubbsuk](https://www.ruby-forum.com/u/stubbsuk)\
**Post date:** [November 9, 2006, 6:06pm UTC](https://www.ruby-forum.com/t/unable-to-create-associations/75320/5 "2006-11-09T18:06:04Z")

</div>

Bryan D. wrote:

> Stuart G. wrote:
> 
> > class User \< ActiveRecord::Base  
> > has\_one :provider
> > 
> > def invoices\_raised  
> > provider.get\_invoices  
> > end
> > 
> > …
> > 
> > end
> 
> What if you change “provider.get\_invoices” to  
> “self.provider.get\_invoices”?

If you omit the receiver, it defaults to self.

i.e. “provider.get\_invoices” is interpreted as being equal to  
“self.provider.get\_invoices”

Here is a good link if you want to learn more about this or see some  
examples:  
[http://www.rubycentral.com/book/tut\_methods.html](http://www.rubycentral.com/book/tut_methods.html)
