I have a single table that two people enter data into. Person A creates the record and I need to specify certain required fields in his form. Person B has a separate form and she fills in additional fields and I need to specify that some of these are required. Since the data is all in one table and since the validations are in the model, won''t Rails complain when person A tries to save? I was getting ready to see what would happen, but I thought I''d ask first. Thanks for the help! Bob -- Posted via http://www.ruby-forum.com/.
If I understand you correctly, there is one table with two distinct types. You could split this into two tables or use Single Table Inheritance. If you use STI, you would put a type field in the table and then have 3 models: BaseClass PersonA < BaseClass PersonB < BaseClass Then the PersonA model and PersonB model would have their own validation, with the common validations in the BaseClass. Hope this helps, Ben On 3/31/06, Bob Boyken <bob@boyken.org> wrote:> > I have a single table that two people enter data into. Person A creates > the record and I need to specify certain required fields in his form. > Person B has a separate form and she fills in additional fields and I > need to specify that some of these are required. > > Since the data is all in one table and since the validations are in the > model, won''t Rails complain when person A tries to save? I was getting > ready to see what would happen, but I thought I''d ask first. Thanks for > the help! > > Bob > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060331/f2971178/attachment-0001.html
Ben Reubenstein wrote:> BaseClass > PersonA < BaseClass > PersonB < BaseClass > > Then the PersonA model and PersonB model would have their own > validation, > with the common validations in the BaseClass. > > Hope this helps, > > BenHe''s an example: When we get a trouble call, the receptionist initiates a ticket. There are certain fields that are mandatory (name, phone, etc.) and certain fields that are optional (title, middle name, etc.). Later, someone else opens the ticket, but with a totally different form that has many additional fields from the table. Again, some of these fields are required and some are optional. I tried the two table approach and it introduced some unpleasant complexities. I will look into single-table inheritance. Thanks! Bob -- Posted via http://www.ruby-forum.com/.
Bob, take a look at the :on parameter to your validations. You can say: validates_something_or_other :my_field, :on => :create you have :on => :save (which is the default) or :on => :create or :on => :update it might fit with the flow you''ve described. HTH Trevor -- Trevor Squires http://somethinglearned.com On 31-Mar-06, at 9:42 AM, Bob Boyken wrote:> Ben Reubenstein wrote: >> BaseClass >> PersonA < BaseClass >> PersonB < BaseClass >> >> Then the PersonA model and PersonB model would have their own >> validation, >> with the common validations in the BaseClass. >> >> Hope this helps, >> >> Ben > > He''s an example: When we get a trouble call, the receptionist > initiates > a ticket. There are certain fields that are mandatory (name, phone, > etc.) and certain fields that are optional (title, middle name, etc.). > Later, someone else opens the ticket, but with a totally different > form > that has many additional fields from the table. Again, some of these > fields are required and some are optional. > > I tried the two table approach and it introduced some unpleasant > complexities. I will look into single-table inheritance. Thanks! > > Bob > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
> Again, some of these fields are required and some are optional.You might also be interested in the :if parameter which allows you to specify a method, proc or string to call to determine if the validation should occur. So you could do something like: validates_presence_of :name, :if => current_user.role == "Receptionist" validates_presence_of :other_attribute, :if => current_user.role == "Technician" Hope that helps. -- DeLynn Berry delynn@gmail.com http://www.delynnberry.com
Trevor Squires wrote:> Bob, > > take a look at the :on parameter to your validations. > > You can say: > > validates_something_or_other :my_field, :on => :create > > you have :on => :save (which is the default) or :on => :create or :on > => :update > > it might fit with the flow you''ve described. > > HTH > TrevorPerfect! This is a simple solution to my problem. Man, I gotta get better at asking questions, because as I think through your answer, and my problem, I think during :create is the only time we''ll be dealing with a partial form. Thanks! -- Posted via http://www.ruby-forum.com/.
Hi again, comments inline: On 31-Mar-06, at 10:32 AM, Bob Boyken wrote:> Trevor Squires wrote: >> Bob, >> >> take a look at the :on parameter to your validations. >> >> You can say: >> >> validates_something_or_other :my_field, :on => :create >> >> you have :on => :save (which is the default) or :on => :create or :on >> => :update >> >> it might fit with the flow you''ve described. >> >> HTH >> Trevor > > Perfect! This is a simple solution to my problem. Man, I gotta get > better at asking questions, because as I think through your answer, > and > my problem, I think during :create is the only time we''ll be dealing > with a partial form. >My example may have been a little misleading tho. In your scenario it''s likely that your going to have the create-only validations have no :on parameter and the update-only validations will specify :on => :update. Kinda backwards from my original example. Trev> Thanks! > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails