# Relations problem

**URL:** <https://www.ruby-forum.com/t/relations-problem/126698>\
**Category:** Rails\
**Created:** [January 21, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/relations-problem/126698 "2008-01-21T14:15:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 21, 2008, 2:15pm UTC](https://www.ruby-forum.com/t/relations-problem/126698/1 "2008-01-21T14:15:32Z")

</div>

I’m using Restful Authentication (which I hope some of you know), so I’m  
using that Users-table as my main table.

Now, I’ve added a table called ‘Presentations’.  
I’ve also added a presentation\_id in my Users-table so the right user’s  
presentation can be found. In my User-model I’ve got a “belongs\_to  
:presentation” and in my Presentation-model “belongs\_to :user”.

As long as I create/assign everything MANUALLY in the tables it works  
fine. Now, that’s kind of dull to do for every new user signing in  
(especially since the presentation page will render a “nil-crash” if I  
haven’t assigned the user a Presentation with som text before he/she  
logges in).

I know that I need to do a create function somewhere, but I’m unsure  
where to put it, though I’m guessing in the UserController’s create. I  
want it to create a default text once (upon creation of the new user) so  
the user later on can edit his/her presentation (a feature I’ve allready  
implemented).

my question: Where do I create this “create” function (to work with  
Restful Authentication)and any suggestions on how should it look/work?

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 21, 2008, 2:52pm UTC](https://www.ruby-forum.com/t/relations-problem/126698/2 "2008-01-21T14:52:06Z")

</div>

On Jan 21, 2008 8:15 AM, Dag S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I’m using Restful Authentication (which I hope some of you know), so I’m  
> using that Users-table as my main table.
> 
> Now, I’ve added a table called ‘Presentations’.  
> I’ve also added a presentation\_id in my Users-table so the right user’s  
> presentation can be found. In my User-model I’ve got a “belongs\_to  
> :presentation” and in my Presentation-model “belongs\_to :user”.

Something’s wrong here.

Presentation :belongs\_to user means that Presentation has a column  
called user\_id which ‘points to’ a User. You dont have this.

And belongs\_to isn’t symmetrical, the other end needs a :has\_many, or  
:has\_one relationship.

From what you started out saying there’s one presentation for each  
user, in which case the relations would be:

User  
has\_one :presentation

Presentation  
belongs\_to :user

with user\_id field in the presentations table.

That said, has\_one is almost never used like this, if there really is  
a fixed one-one relationship between presentations and users then they  
probably should be combined in one table. If, on the other hand, the  
situation is that the User can have multiple presentations then you  
might want something like:

User  
has\_many :presentations  
has\_one :current\_presentation, :order =\> “updated\_at DESC”

where the current\_presentation uses the magic updated\_at field to get  
the most recently modified presentation.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 21, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/relations-problem/126698/3 "2008-01-21T15:44:23Z")

</div>

> From what you started out saying there’s one presentation for each  
> user, in which case the relations would be:
> 
> User  
> has\_one :presentation
> 
> Presentation  
> belongs\_to :user
> 
> with user\_id field in the presentations table.

Allright, well this at least did make the relations clearer, thanks  
Rick.

Still I’m not very clear about how to create a new presentation with a  
default value as a new user gets created (especially since the datatype  
TEXT dosen’t allow default values)?

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 22, 2008, 11:07am UTC](https://www.ruby-forum.com/t/relations-problem/126698/4 "2008-01-22T11:07:28Z")

</div>

@presentation = current\_user.presentation  
@presentation = current\_user.presentation.create unless @presentation

Now, all it does is it returns nil (I hate nil, btw because it’s allways  
so “unexpected”).

The error msg beeing ([http://localhost:3000/users](http://localhost:3000/users)):  
“You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.create”

ActiveRecord::Base expected an instance…?

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 22, 2008, 11:55am UTC](https://www.ruby-forum.com/t/relations-problem/126698/5 "2008-01-22T11:55:27Z")

</div>

2008/1/22, Dag S. :

> @presentation = current\_user.presentation  
> @presentation = current\_user.presentation.create unless @presentation
> 
> Now, all it does is it returns nil (I hate nil, btw because it’s allways  
> so “unexpected”).

@presentation = current\_user.create\_presentation(…)

– Jean-François.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 21, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/relations-problem/126698/6 "2008-01-21T16:53:35Z")

</div>

Another thing I’ve tried is to created a new Presentation object to the  
current\_user with:

@presentation = current\_user.Presentation.new  
@presentation.save!

by placing it in the UserController in the class definition “create”  
(which already exists)but this only generates half of what I want to  
accomplish:  
new rows,  
but sadly enough only NULL in the columns.

---

<div class="post-metadata">

**Author:** ![kaizerkaizer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/45deac/32.png) [@kaizerkaizer](https://www.ruby-forum.com/u/kaizerkaizer)\
**Post date:** [January 22, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/relations-problem/126698/7 "2008-01-22T12:19:14Z")

</div>

Jean-FranÃ§ois TrÃ¢n wrote:

> 2008/1/22, Dag S. :
> 
> > @presentation = current\_user.presentation  
> > @presentation = current\_user.presentation.create unless @presentation
> > 
> > Now, all it does is it returns nil (I hate nil, btw because it’s allways  
> > so “unexpected”).
> 
> @presentation = current\_user.create\_presentation(…)
> 
> – Jean-Franï¿½ois.

Thank you, Fran?ois!  
saved my day
