Conditional Validation and restful_authentication

I'm using the restful_authentication plugin/generator to user access
control in an app I'm working on. I added on to the user model for
things like male/female. I have two methods on the user model male?
and female?. I have two fields in a model that has a one-to-many
relationship with the User model that are only needed if the
current_user.female? is true. However I'm not sure how I would call
the method from a model that is not the User model. Can anyone offer
some advice?

Thanks,

Tim K.

try current_user.male? and current_user.female?

check out lib/authenticated_system.rb in your project. this will give
you some insight on how to work with logged in users.

good luck.
mike

sorry, I misread your question...

No problem Michael, yeah - I've tried that... The only way I can
think of doing it is assigning an instance variable that I can check
from the model. But that just doesn't seem right. Anyone else have
any thoughts?

Sorta Ryan... more like

validates_presence_of :ovaries, :if => :female?

However ovaries is in one model and :female? is a method on the user
model.

Do you have a relationship (belongs_to, has_many) set up on these
other models with your User model?

Could you pass a value into a method on these other classes and then
use a dynamic finder on User?

OtherClass

def my_method name
  u = User.find_by_username name
end

It's tough to figure it out without knowing how these other models are
set up.

Michael, there is definitely a relationship. Here let me give you
some better details. I'm calculating lean body mass which is
different for both men and women. I have the User model that
restful_authentication generates with a few added fields one of which
is User.sex, and a has_many :measurements relationship. I then have
two methods in the generated User model as such:

def male?
  self.sex == "male"
end

def female?
  self.sex == "female"
end

I have a Measurements model which has a relationship with the user
using belongs_to :user. I have two fields that are I want to validate
the presence of only in the case were current_user.female? is true.

Hope that clears it up a little more.

Tim

validates_presence_of :wrist_inches, :if => :female?

Results in:

undefined method `female?' for #<Measurement:0x1123378>

Remember female? is a method of a different model. I think the issue
here is really how do I call the current_user object that restful_auth
sets as a local variable from other models. Because logic tells me I
could create a method on the the Measurements model that does:

def current_user_is_female
  User.find_by_id(current_user.id).female?
end

and then doing:

validates_presence_of :wrist_inches, :if => :current_user_is_female

But doing that of course leaves me with a "undefined local variable or
method `current_user' for #<Measurement:0x1736bac>"

Tim

Awesome Ryan - that was exactly it!

I needed to make a another definition for female? -- Perfect! :smiley:

You rock man!

Tim