I’ve got a load of users and they have adverts, such that a User has
many adverts and an Advert belongs to a user. I’m going to need to stop
users creating more than a certain number of adverts based on their
status. I’ve got things working ok but it’s a bit inelegant as I’m doing
it with before filters in the controller and at present the users have
no status (one step at a time so 1st a blanket rule to ban more than
three adverts).
I’m guessing that my approach is sound (scuse this syntax if it’s wrong,
just typing the code by hand into this forum without my textmate)
this works @user.adverts.count
Something like
in the adverts controller
before_filter :check_user_has_not_gone_over_advert_limit, :except =>
[:show, :delete]
and in the Advert model (or should this be in the User model)
def check_user_has_not_gone_over_advert_limit
if current_user
if current_user.adverts.count > 3
false # fail and flash a messasge that advert count is breached.
else
true # all is fine
end
else
false # not logged in so not allowed to do stuff with adverts,
register first
end
end
I know the code is wrong but is the approach on the right lines?
bb
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
else
all is normal and the user can add adverts
end
I can’t see a need to do this in before filters.
Trausti
On Thu, Aug 6, 2009 at 10:30 AM, bingo bob
<[email protected]
Aha ! didn’t think of this, looks like I’ve overcomplicated things - of
course !
But what do you mean by the add action ? I don’t have one.
Do you mean the new action in the adverts controller or maybe the create
action in the adverts controller or actually a new add action ?
bb
Yes, the action in your adverts controller to create new adverts.
I usually call these add, not very restful I know.
Trausti
On Thu, Aug 6, 2009 at 11:44 AM, bingo bob
<[email protected]
OK, but the New action or the Create ?
New it is then!
and thanks, that’s working a treat. Imagine this is more simple than my
previously over-engineered idea.
On Aug 6, 6:23 am, bingo bob [email protected] wrote:
New it is then!
and thanks, that’s working a treat. Imagine this is more simple than my
previously over-engineered idea.
Posted viahttp://www.ruby-forum.com/.
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.
Ok, I’m sold - I’ll put it in the model - user model I take it?
What’s the maxed_adverts? method look like then ? and how do i call it
from the create action in the adverts_controller ?
On Aug 6, 3:30 pm, bingo bob [email protected] wrote:
Ok, I’m sold - I’ll put it in the model - user model I take it?
What’s the maxed_adverts? method look like then ? and how do i call it
from the create action in the adverts_controller ?
Posted viahttp://www.ruby-forum.com/.
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.
Thanks,
I’ve followed your advice (created a maxed_out_adverts?) method in the
user model. Works a treat. While I was at it I added a
User.max_allowed_adverts column in my DB. I’m quite pleased with myself.
However I think the adverts_controller could defo do with a critical eye
being cast upon it. It’s here. Comments? Am I right with this approach
to before filters - I suspect some of those actions called by the before
filters would be better placed in application_controller ?
Pastie here with line numbers.
http://pastie.org/575239
bb
Anyone critique that controller - I’d be most gateful.
When a user wants to add an advert, what action does he call first ?
Trausti
On Thu, Aug 6, 2009 at 11:53 AM, bingo bob
<[email protected]