SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast Stevenwulf's Avatar
    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
    So lets say I've created an Installer model and associated various operating systems with it. Now I want to edit the Installer model. In my form, I'm displaying all the available operating systems via checkboxes.

    [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

  2. #2
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the action that is being processed when you click "update"?

  3. #3
    SitePoint Enthusiast Stevenwulf's Avatar
    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

  4. #4
    Resident Java Hater
    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

  5. #5
    SitePoint Enthusiast Stevenwulf's Avatar
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •