i have a user model with fields name and sex. And each user can have an one-to-one association called "father" with a male user. So in user model i added a field father_id and added association like this, class User < ActiveRecord::Base belongs_to :father, :class_name => ''User'', :inverse_of => :user, :foreign_key => "father_id" has_one :user, :class_name => ''User'', :inverse_of => :father SEX=["male", "female"] validates :sex, :presence => true,:inclusion => { :in => SEX } end now it can make both female user and male user as father. How could i restrict it to make user as father only whose sex is male? -- 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-/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 https://groups.google.com/groups/opt_out.
On Thu, Nov 22, 2012 at 5:53 PM, ariv arasan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> i have a user model with fields name and sex. And each user can have an > one-to-one association called "father" with a male user. > > So in user model i added a field father_id and added association like > this, > > class User < ActiveRecord::Base > belongs_to :father, :class_name => ''User'', :inverse_of => :user, > :foreign_key => "father_id" > has_one :user, :class_name => ''User'', :inverse_of => :father > > SEX=["male", "female"] > > validates :sex, :presence => true,:inclusion => { :in => SEX } > > end >hmm i think you just need to define a custom validation validates :validates_father_is_a_male private def validates_father_is_a_male errors.add(:father_id, ''is not a male'') if father && father.sex == ''male'' end> > now it can make both female user and male user as father. How could i > restrict it to make user as father only whose sex is male? > > -- > 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-/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 https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/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 https://groups.google.com/groups/opt_out.
yes, custom validation helps me. i modified your code a little for my need, validate :validates_father_is_a_male private def validates_father_is_a_male errors.add(:father_id, ''is not a male'') if father && father.sex =''female'' end Thanks a lot! -- 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-/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 https://groups.google.com/groups/opt_out.
On 22 November 2012 10:51, ariv arasan <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> yes, custom validation helps me. > > i modified your code a little for my need, > > validate :validates_father_is_a_male > > private > > def validates_father_is_a_male > errors.add(:father_id, ''is not a male'') if father && father.sex => ''female'' > endDon''t forget to allow for some of the less conventional situations that can occur in modern life where male and female conditions are not immutable and parents may not necessarily be of different sexes. It is possible for someone classed as female to also be a father. It may be better to allow the user to worry about these things rather than force validations on them. Colin -- 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 https://groups.google.com/groups/opt_out.
@Colin sure, will consider it. Thanks -- 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-/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 https://groups.google.com/groups/opt_out.