I’m trying to follow Fat Model Thin Controller, and generally just do
things right as much as possible and need some help at this early stage
of development.
I have some users (courtesy of restful_authentication).
I’ve got my relationships such that a User has_many Adverts and an
Advert belongs_to a user.
I have an adverts controller, and I’m structuring things as it stands
such that the index action gets me all the ads for the currently logged
in user (“current_userâ€).
I have this working with the following code, as I say in the adverts
controllers index action.
@adverts = current_user.adverts
Now then, I figure that in the correct place to do the finding of a
users adverts is in the adverts model. This should give me the ability
to do the find and ordering in lots of different ways in the future, and
I believe it’s just the correct way to go.
So now I have in my controller index action
@adverts = Advert.find_adverts_for_current_user
and that works.
Now the thing is, couple of things I need help with
when a user hasn’t got any adverts I get errors, my thoughts are just if
statement in either the controller or the model which only does the find
when the user has adverts and does something else sensible (“displays a
flash†perhaps) when there are none. Do I do this in the model or the
controller and what’s the syntax – I tried repeatedly this morning but
couldn’t get it. I figure the model is the right place, but I can’t
display a flash with the model!
How do I elegantly make sure that you can only delete your own adverts
(apart from admin who can delete anything of course).
And I suppose when a user is deleted their adverts should be eradicated
with them!
Quite a bit to think about here, I would very much appreciate any help!
bb.