Hi all, I am newbie in Rails and new at this group. I am with a problem with validation. I searched at Google but I cant see anything. I have just one Model: User. I have 2 CRUD''s (controllers and views): Representatives and Users... Users CRUD is ok but Representatives not. I have different validations rules for Representatives and Users, but I have to use the same table in the DB, so the same Model. How can I make different validations in a same Model? Thanks a lot! -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Apr 26, 2011, at 9:35 AM, Junior Grossi wrote:> Hi all, > > I am newbie in Rails and new at this group. I am with a problem with > validation. I searched at Google but I cant see anything. > > I have just one Model: User. I have 2 CRUD''s (controllers and views): > Representatives and Users... Users CRUD is ok but Representatives not. > I have different validations rules for Representatives and Users, but > I have to use the same table in the DB, so the same Model. > > How can I make different validations in a same Model?Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models... -philip -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi philip!!! Thanks for your reply. I read about STI and solve my problem. BUT now I have another hehe. I have 2 validations rules, because I have a has_one association. How can I do this? http://pastie.org/1836266 For example... I have to validate :name (Profile) for Representative, but not for User. Thanks! On Apr 26, 1:58 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> On Apr 26, 2011, at 9:35 AM, Junior Grossi wrote: > > > Hi all, > > > I am newbie in Rails and new at this group. I am with a problem with > > validation. I searched at Google but I cant see anything. > > > I have just one Model: User. I have 2 CRUD''s (controllers and views): > > Representatives and Users... Users CRUD is ok but Representatives not. > > I have different validations rules for Representatives and Users, but > > I have to use the same table in the DB, so the same Model. > > > How can I make different validations in a same Model? > > Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models... > > -philip-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
> Thanks for your reply. I read about STI and solve my problem. > > BUT now I have another hehe. I have 2 validations rules, because I > have a has_one association. How can I do this? > > http://pastie.org/1836266 > > For example... I have to validate :name (Profile) for Representative, > but not for User.class Profile... validates :name, :presence => {:if => lambda {|p| p.representive.present?}} end Or something like that... might need to check the class type instead... that should get you started though.> Thanks! > > > On Apr 26, 1:58 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote: >> On Apr 26, 2011, at 9:35 AM, Junior Grossi wrote: >> >>> Hi all, >> >>> I am newbie in Rails and new at this group. I am with a problem with >>> validation. I searched at Google but I cant see anything. >> >>> I have just one Model: User. I have 2 CRUD''s (controllers and views): >>> Representatives and Users... Users CRUD is ok but Representatives not. >>> I have different validations rules for Representatives and Users, but >>> I have to use the same table in the DB, so the same Model. >> >>> How can I make different validations in a same Model? >> >> Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models... >> >> -philip > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Thanks! I will try do something like that. []s On Apr 26, 7:43 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote:> > Thanks for your reply. I read about STI and solve my problem. > > > BUT now I have another hehe. I have 2 validations rules, because I > > have a has_one association. How can I do this? > > >http://pastie.org/1836266 > > > For example... I have to validate :name (Profile) for Representative, > > but not for User. > > class Profile... > > validates :name, :presence => {:if => lambda {|p| p.representive.present?}} > > end > > Or something like that... might need to check the class type instead... that should get you started though. > > > > > > > > > Thanks! > > > On Apr 26, 1:58 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote: > >> On Apr 26, 2011, at 9:35 AM, Junior Grossi wrote: > > >>> Hi all, > > >>> I am newbie in Rails and new at this group. I am with a problem with > >>> validation. I searched at Google but I cant see anything. > > >>> I have just one Model: User. I have 2 CRUD''s (controllers and views): > >>> Representatives and Users... Users CRUD is ok but Representatives not. > >>> I have different validations rules for Representatives and Users, but > >>> I have to use the same table in the DB, so the same Model. > > >>> How can I make different validations in a same Model? > > >> Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models... > > >> -philip > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Just to complete the topic... solved! Solution: validates_presence_of :name, :tel_commercial, :tel_mobile, :address, :city, :state, :neighborhood, :if => :representative? def representative? lambda {|p| p.representative.present?} end Thanks philip! []s On Apr 27, 9:41 am, Junior Grossi <juninh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks! I will try do something like that. > > []s > > On Apr 26, 7:43 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote: > > > > > > > > > > Thanks for your reply. I read about STI and solve my problem. > > > > BUT now I have another hehe. I have 2 validations rules, because I > > > have a has_one association. How can I do this? > > > >http://pastie.org/1836266 > > > > For example... I have to validate :name (Profile) for Representative, > > > but not for User. > > > class Profile... > > > validates :name, :presence => {:if => lambda {|p| p.representive.present?}} > > > end > > > Or something like that... might need to check the class type instead... that should get you started though. > > > > Thanks! > > > > On Apr 26, 1:58 pm, Philip Hallstrom <phi...-LSG90OXdqQE@public.gmane.org> wrote: > > >> On Apr 26, 2011, at 9:35 AM, Junior Grossi wrote: > > > >>> Hi all, > > > >>> I am newbie in Rails and new at this group. I am with a problem with > > >>> validation. I searched at Google but I cant see anything. > > > >>> I have just one Model: User. I have 2 CRUD''s (controllers and views): > > >>> Representatives and Users... Users CRUD is ok but Representatives not. > > >>> I have different validations rules for Representatives and Users, but > > >>> I have to use the same table in the DB, so the same Model. > > > >>> How can I make different validations in a same Model? > > > >> Look into STI (single table inheritance). This will let you use the same underlying table (and share common functionality) among multiple models... > > > >> -philip > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.