counting a users adverts and then doing stuff

As yo explain this problem, you could just check the number of adverts the user has in the add function. Something like this pseudo code

def add

adverts = User.adverts

if adverts.length > 3

flash too many adverts

redirect back


all is normal and the user can add adverts


I can’t see a need to do this in before filters.


Yes, the action in your adverts controller to create new adverts.

I usually call these add, not very restful I know.


When a user wants to add an advert, what action does he call first ?


New it is then!

and thanks, that's working a treat. Imagine this is more simple than my
previously over-engineered idea.

And then when the user POSTs directly to your create action, your
maximum advert limit is for naught. It might *seem* to make more sense
to put this in the create action, where the advertisement is actually
getting persisted to the DB or whathaveyou, but even then this seems
like domain specific logic that really should go in your model. (I
know I'm getting waaaay ahead of myself here, but what if this one
specific controller ends up not being the only place you'll have users
be able to create adverts from? More importantly I guess, keeping this
in the model makes everything *much* easier to spec). A simple
"maxed_adverts?" method and an "create_avert" method in the model
which checks the prior would do nicely.

The User model does sound reasonable.

maxed_adverts? looks like something that checks if the user has maxed
their advert limit. Maybe it looks like "def maxed_averts?;
averts.count >= 3; end"? Maybe it looks different.

Call it like you would any other method on your models... I guess it
helps when your starting out to always remember that Rails "models"
are nothing more than instances of Ruby classes which *usually*
descend from ActiveRecord::Base. Nothing magical about them but the
magic you give them.