SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating multiple models

    Hi

    I have fairly complex form which updates 5 different tables in my database. There are 5 sections of the form which have their visibility toggled on and off based on previous selections. Each section updates a different table and I only want each section to update the table if it is visible and the data entered is valid. At the moment, if the user clicks submit all the tables are updated even if certain parts of the form are not applicable.

    Does anyone know how I can get the form to update the tables, only if the section is visible and valid?

    Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Model objects have the method valid? which you could test before saving:
    Code:
    thing.save if other_thing.valid?
    The valid? method runs the validation scripts for the model object.

    I'd also suggest you look at creating a method that does the necessary checks and saving, as you don't want to be recoding the same validations all the time.

    It would also be worth checking the Transaction options if you are looking at multiple updates.

  3. #3
    SitePoint Member
    Join Date
    May 2008
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Angry nice

    Nice topic, nice article.


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
  •