Relations problem

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?

On Jan 21, 2008 8:15 AM, Dag S. [email protected]
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/

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)?

@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):
“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…?

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.

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.

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