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?
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?
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.
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