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/.
Trausti Thor Johannsson
2009-Aug-06 09:07 UTC
Re: 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 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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/.
Trausti Thor Johannsson
2009-Aug-06 09:51 UTC
Re: counting a users adverts and then doing stuff
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
OK, but the New action or the Create ? -- Posted via http://www.ruby-forum.com/.
Trausti Thor Johannsson
2009-Aug-06 10:10 UTC
Re: counting a users adverts and then doing stuff
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > OK, but the New action or the Create ? > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
New it is then! and thanks, that''s working a treat. Imagine this is more simple than my previously over-engineered idea. -- Posted via http://www.ruby-forum.com/.
On Aug 6, 6:23 am, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 ? -- Posted via http://www.ruby-forum.com/.
On Aug 6, 3:30 pm, bingo bob <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> 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 -- Posted via http://www.ruby-forum.com/.