SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Hybrid View
-
Feb 10, 2006, 18:11 #1
- Join Date
- May 2002
- Location
- Berkeley
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Lifecycle of HABTM related models
I have the following models
Installer
OperatingSystem
SupportedOperatingSystem [intersection entity]
An Installer can have many OperatingSystems, the operating systems that it supports are maintained in SupportedOperatingSystem model. It is required that and Installer be associated with at least 1 OperatingSystem.
validation code in Installer model
Code:def validate if self.operating_systems.empty? errors.add_to_base('You must select at least one Operating System') end end
[x]os1 [ ]os2 [ ]os3
os1 is checked because when I created the Installer model I associated it with os1. So now I uncheck os1 and click "update." I get an error message as expected "You must select at least one Operating System." But, if I check the "supported_operating_systems" table, this association has been deleted, even though the validation failed. This is bad, because if the user decides to close their browser then the Installer model will be left in an invalid state. Is there a way to prevent this? Why is the record being deleted even though validation does not succeed?
Thanks,
Steven
-
Feb 10, 2006, 22:08 #2
Can you post the action that is being processed when you click "update"?
-
Feb 11, 2006, 09:02 #3
- Join Date
- May 2002
- Location
- Berkeley
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Here is the update action:
Code:def update if params[:installer][:operating_system_ids].nil? params[:installer][:operating_system_ids] = [] end i = @installer = Installer.find(params[:id]) if @installer.update_attributes(params[:installer]) flash[:notice] = 'Installer was successfully updated.' redirect_to(:controller => 'software', :action => 'show', :id => i.software) else self.populate_selection_values render(:action => 'edit') end end
-
Feb 11, 2006, 16:16 #4
- Join Date
- Jul 2004
- Location
- Gerodieville Central, UK
- Posts
- 446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I dunno if it has anything to do with the problem or not, but I would avoid explict method defnition for validators, ather I would rename the validation method (if there are lots of validation rules I normally break them down into related methods), and then say you call you method ensure_operating_system_selected I would do something like...
validate :ensure_operating_system_selected
as a meta-method
and on an unrealted note ...
params[:installer][perating_system_ids] ||= []
can be used to eliminate the if nil statement.
If you continue to have problem, stick a raise class in the validation process and look at the stack trace, or use a breakpoint command and load up a breakpoint console to poke about and investigate the program state
-
Feb 12, 2006, 10:01 #5
- Join Date
- May 2002
- Location
- Berkeley
- Posts
- 76
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I've actually set up my validation by defining a callback as you suggested, so I don't think that's the problem.
However, thanks for your suggestion:
params[:installer][operating_system_ids] ||= []
is much more consise.
Bookmarks