I know in the model we can add validations. But how about if I need different validations depending on the controller. For example... 1) if the User is being created from the accounts controller, I want to REQUIRE the username be entered. 2) if the User is being created from the users controller, I want to NOT REQUIRE the username. validates_presence_of :username, :on => :create requires the username regardless of the controller being used. -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 21 July 2011 20:53, Leonel *.* <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> I know in the model we can add validations. But how about if I need > different validations depending on the controller. > > For example... > 1) if the User is being created from the accounts controller, I want to > REQUIRE the username be entered. > 2) if the User is being created from the users controller, I want to NOT > REQUIRE the username. > > validates_presence_of :username, :on => :create requires the username > regardless of the controller being used.Just to make sure I understand the requirement, you want to be able to create a user record in the database with an empty user name if it is being created in the users controller? It seems a bit odd to have a user record in the database with no username. 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Sorry, I should''ve explained more. This is the general idea. There is a Sign Up form which creates 3 records in 3 different models: Account Company (belongs to account) User (belongs to user) So the Sign Up form, creates the user from the accounts_controller.rb @account = Account.new company = @account.companies.build company.users.build Now, once the sign up is complete, the user can invite other people to user the web app. So he adds users, the app sends an invitation email and when the person clicks on the invitation email, it gives it a form to choose desired username. In summary... 1) when the user is being created from the accounts controller (sign up), REQUIRE username 2) when the user is adding other users from users controller (add user), DO NOT REQUIRE username -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Jul 21, 9:13 pm, "Leonel *.*" <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Sorry, I should''ve explained more. This is the general idea. > > There is a Sign Up form which creates 3 records in 3 different models: > Account > Company (belongs to account) > User (belongs to user) > > So the Sign Up form, creates the user from the accounts_controller.rb > @account = Account.new > company = @account.companies.build > company.users.build > > Now, once the sign up is complete, the user can invite other people to > user the web app. So he adds users, the app sends an invitation email > and when the person clicks on the invitation email, it gives it a form > to choose desired username. > > In summary... > 1) when the user is being created from the accounts controller (sign > up), REQUIRE username > 2) when the user is adding other users from users controller (add user), > DO NOT REQUIRE username >I don''t think you want to think of this as a controller based thing. Apart from anything else, if you at some later point want to update a user object then you''ll need to know what validations to apply. Instead, you could have a state column, and apply validations based on that state. When the accounts controller creates a user, it sets its state to (for example) signed_up, and that state requires all your validation, whereas in the second case the controller could set the state to ''invited'', which has a separate set of validations. (if you do go down this route, the aasm gem can help you manage states and the transitions between them) Fred -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
> Instead, you could have a state column, and apply validations based on > that state. When the accounts controller creates a user, it sets its > state to (for example) signed_up, and that state requires all your > validation, whereas in the second case the controller could set the > state to ''invited'', which has a separate set of validations. (if you > do go down this route, the aasm gem can help you manage states and the > transitions between them)Ok, let''s say I do add a state column. How would I setup the validation, you mean, in the model check the state and apply validation according to the state? Would you recommend I set the state column... 1) as string or 2) as a foreign key to a states tables that lists the states? -- 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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.