ruby on rails - Changing validation rules on-the-fly -
I need conditional verification in some parts of my app right now I am using the following plan:
User Create User :: withPassword.create User :: WithPhone.create
It would be nice if I could change the class behavior when I fly:
User.with_phone.with_password.create
So I tried to do this:
class user & lt; ActiveRecord :: Base valid: phone, presence: true, if: phone_require? Def self.with_phone define_method (: phone_required?) {True} Need a self-def personal def phone? Incorrect end term
Then it can be used like this where:
user.with_phone.create (user_params)
The problem with this approach is that all the examples of users get new behavior since the actual class changes.
Is there a way to return only the modified copy of the Updates Thank you for this comment is more about an idea, the requirement is that I have users without something Automatically build assumptions, and then when they edit the profile, they are working with the old Here's my next visit: < / P> Apparently this is not any better because the singleton system will always be there. user
class phone_required?
user
model
class user & lt; ActiveRecord :: Base valid: phone, presence: true, if: phone_require? Def self.with_password define_singleton_method (: password_required?) {True} self end DEF password_required? Self.class.try: password_required? Finally
Why not only use an initial frequency at construction time?
class user & lt; ActiveRecord :: Base valid: phone, presence: true, if: phone_require? @phone_required = false def self.create_with_phone (consultation) obj = self.create (params) obj.phone_required = true end personal def phone_required = (v) @phone_required = v end def phone_required? @phone_required End End User.create_with_phone (user_params)
Comments
Post a Comment