Hi guys n'' gals.
Quick Q on this... it should be easy, but it''s not...
I''ve got users and members.... and a Member has_many Users. I want to
have validation that ensures that the member has at least one user.
validates_presence_of :user doesn''t work because the user and member
aren''t associated until after a save! is called ( the member_id field
of User stays nil ).
I tried using validates as follows:
validate :must_have_user
def must_have_user
errors.add_to_base("Must have at least one user") unless
self.users.count > 0
end
but member.users.count stays at 0, even though I used
member.users.build to create the user.
Any ideas on how I can get this to validate? Rails obviously knows
that the user and member are related... how can I confirm this?
Thanks!
Randal
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
On 5-Jun-08, at 2:14 PM, sw0rdfish wrote:> > Hi guys n'' gals. > > Quick Q on this... it should be easy, but it''s not... > > I''ve got users and members.... and a Member has_many Users. I want to > have validation that ensures that the member has at least one user. > > validates_presence_of :user doesn''t work because the user and member > aren''t associated until after a save! is called ( the member_id field > of User stays nil ). > > I tried using validates as follows: > > validate :must_have_user > > def must_have_user > errors.add_to_base("Must have at least one user") unless > self.users.count > 0 > end > > but member.users.count stays at 0, even though I used > member.users.build to create the user. > > Any ideas on how I can get this to validate? Rails obviously knows > that the user and member are related... how can I confirm this?use User.find instead of the association Jodi --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
and I''m a goof...
just remembered that members.users.count would query the DB, which is
0... however if you use members.users.size, it''ll query the object in
memory :)
SO my validate method is...
def must_have_user
errors.add_to_base("Must have at least one user") unless
self.users.size > 0
end
and then it works :)
I''ll be happy to take any more advice on this though, thanks :)
On Jun 5, 2:17 pm, Jodi Showers
<j...-BOB1p6JRLoAV+D8aMU/kSg@public.gmane.org>
wrote:> On 5-Jun-08, at 2:14 PM, sw0rdfish wrote:
>
>
>
>
>
> > Hi guys n'' gals.
>
> > Quick Q on this... it should be easy, but it''s not...
>
> > I''ve got users and members.... and a Member has_many Users.
I want to
> > have validation that ensures that the member has at least one user.
>
> > validates_presence_of :user doesn''t work because the user and
member
> > aren''t associated until after a save! is called ( the
member_id field
> > of User stays nil ).
>
> > I tried using validates as follows:
>
> > validate :must_have_user
>
> > def must_have_user
> > errors.add_to_base("Must have at least one user") unless
> > self.users.count > 0
> > end
>
> > but member.users.count stays at 0, even though I used
> > member.users.build to create the user.
>
> > Any ideas on how I can get this to validate? Rails obviously knows
> > that the user and member are related... how can I confirm this?
>
> use User.find instead of the association
>
> Jodi
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---