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
-- 
Posted via http://www.ruby-forum.com/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to