hello all, i have a model that has a field ''votes''. users may only vote once per session and so i am using a session variable to track whether they have voted. i would like to use rails validation within the model to output an error if a user tries to vote more than once. my issue is that this tracking of whether the user has voted this session is not tracked in the database (it is in the session variable). is it still possible to use the rails validation framework to validate? i am doing the appropriate checking in the controller, i''m just not sure how to then propagate this check to the model (in order to use the validation framework). can i just define a new attribute for the model even though it is not a field in the db? thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
i realized that the post may have not been totally clear: basically, i just want to validate against something that is not in the db/model - is this possible to do in the model so that i have access to the Error object? On Sep 17, 2:11 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> hello all, > > i have a model that has a field ''votes''. users may only vote once per > session and so i am using a session variable to track whether they > have voted. i would like to use rails validation within the model to > output an error if a user tries to vote more than once. > > my issue is that this tracking of whether the user has voted this > session is not tracked in the database (it is in the session > variable). is it still possible to use the rails validation framework > to validate? i am doing the appropriate checking in the controller, > i''m just not sure how to then propagate this check to the model (in > order to use the validation framework). can i just define a new > attribute for the model even though it is not a field in the db? > > thanks.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Erol Fornoles
2008-Sep-17 08:24 UTC
Re: how to validate against a field that is not in the model?
On Sep 17, 2:28 pm, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i realized that the post may have not been totally clear: > > basically, i just want to validate against something that is not in > the db/model - is this possible to do in the model so that i have > access to the Error object?Are you referring to something like this? class Session attr_accessor :match # match does not exist in the db, that''s why we defined a attr_accessor for it validates_presence_of :match end --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Why don''t you pass the session value as a parameter to a model''s method? In your controller you would have something like: model.exceeded_vote_count?(session[:vote_count]) In your model: def Model.exceeded_vote_count?(count) count > MAX_VOTE_COUNT ? true : false end Pepe On Sep 17, 2:28 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> i realized that the post may have not been totally clear: > > basically, i just want to validate against something that is not in > the db/model - is this possible to do in the model so that i have > access to the Error object? > > On Sep 17, 2:11 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > hello all, > > > i have a model that has a field ''votes''. users may only vote once per > > session and so i am using a session variable to track whether they > > have voted. i would like to use rails validation within the model to > > output an error if a user tries to vote more than once. > > > my issue is that this tracking of whether the user has voted this > > session is not tracked in the database (it is in the session > > variable). is it still possible to use the rails validation framework > > to validate? i am doing the appropriate checking in the controller, > > i''m just not sure how to then propagate this check to the model (in > > order to use the validation framework). can i just define a new > > attribute for the model even though it is not a field in the db? > > > thanks.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, I should have written: exceeded_count = Model.exceeded_vote_count?(session[:vote_count]) On Sep 17, 12:32 pm, pepe <P...-gUAqH5+0sKL6V6G2DxALlg@public.gmane.org> wrote:> Why don''t you pass the session value as a parameter to a model''s > method? > > In your controller you would have something like: > > model.exceeded_vote_count?(session[:vote_count]) > > In your model: > > def Model.exceeded_vote_count?(count) > count > MAX_VOTE_COUNT ? true : false > end > > Pepe > > On Sep 17, 2:28 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > i realized that the post may have not been totally clear: > > > basically, i just want to validate against something that is not in > > the db/model - is this possible to do in the model so that i have > > access to the Error object? > > > On Sep 17, 2:11 am, arifb <arif.pub...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > hello all, > > > > i have a model that has a field ''votes''. users may only vote once per > > > session and so i am using a session variable to track whether they > > > have voted. i would like to use rails validation within the model to > > > output an error if a user tries to vote more than once. > > > > my issue is that this tracking of whether the user has voted this > > > session is not tracked in the database (it is in the session > > > variable). is it still possible to use the rails validation framework > > > to validate? i am doing the appropriate checking in the controller, > > > i''m just not sure how to then propagate this check to the model (in > > > order to use the validation framework). can i just define a new > > > attribute for the model even though it is not a field in the db? > > > > thanks.--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---