Hi- This is more of a DB question...but say I have a model that has a field that I want to populate with specific items, see below for an example. What is the best way to do this in Rails? Model: name, type, date - where "type" must be one of "male", "female". Do I do this in the DB through constraints, or in the Rails app? Thanks for the feedback! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
You can to the male/female checking in your model. Add this at the top: validates_inclusion_of :type, :in => %w( male female ), :message => "woah! what are you then!??!!" On Mon, Mar 3, 2008 at 9:26 AM, pete <peterbattaglia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi- > > This is more of a DB question...but say I have a model that has a > field that I want to populate with specific items, see below for an > example. What is the best way to do this in Rails? > > Model: > name, type, date - where "type" must be one of "male", "female". > > Do I do this in the DB through constraints, or in the Rails app? > > Thanks for the feedback! > > >-- Henry http://www.henrywagner.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
> You can to the male/female checking in your model. Add this at the top: > > validates_inclusion_of :type, :in => %w( male female ), :message => "woah! > what are you then!??!!"Be wary the word "type" unless you''re using STI or something else Rails specific. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Haha, OK, I like that. But what if someone connects to the DB from outside of the Rails app and populates the DB from another source? There is no constraint on the table, right? PS I am not using "type", just an example. On Mar 3, 6:07 pm, "toby privett" <tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > You can to the male/female checking in your model. Add this at the top: > > > validates_inclusion_of :type, :in => %w( male female ), :message => "woah! > > what are you then!??!!" > > Be wary the word "type" unless you''re using STI or something else > Rails specific.--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Correct, the db would not have the constraint on it. In that case you probably want a constraint at both the db and model level. The nice thing about using the model level constraint is it uses Rails validation and will give a more user friendly error message than having to deal with a db level constraint violation, and present that error to the user. On Mon, Mar 3, 2008 at 11:13 AM, pete <peterbattaglia-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Haha, OK, I like that. > > But what if someone connects to the DB from outside of the Rails app > and populates the DB from another source? There is no constraint on > the table, right? > > PS I am not using "type", just an example. > > On Mar 3, 6:07 pm, "toby privett" <tobypriv...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > You can to the male/female checking in your model. Add this at the > top: > > > > > validates_inclusion_of :type, :in => %w( male female ), :message => > "woah! > > > what are you then!??!!" > > > > Be wary the word "type" unless you''re using STI or something else > > Rails specific. > > >-- Henry http://www.henrywagner.org/ --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---