SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast davidklaw's Avatar
    Join Date
    Jul 2004
    Location
    Detroit
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Rails: Added related models to a "new" model... ?

    The topic doesn't quite explain what I'm trying to do. Picture this...

    I have 2 Models - Manufacturers and Types. Manufacturers have many Types. Rather than forcing the user to create the two on seperate pages I'm aiming to combine them in such a way that I can (using RESTful actions) generate a new Manufacturer and add Types to it before the Manufacturer is saved to the database. The idea is the Types don't actually get saved until the Manufacturer is saved.

    So my 'new' Manufacturer view contains a AJAX form to add Types but within the controller action I can't seem to figure out how to add these new Types to the Manufacturer model in the view.

    Is this even possible? I figured that I could somehow create the Type and push it to the Manufacturer.types but I don't know how to pass the Manufacturer model along to do this.

    Any help would be much appreciated!
    David K.

  2. #2
    SitePoint Enthusiast davidklaw's Avatar
    Join Date
    Jul 2004
    Location
    Detroit
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Me again, still looking for input but one possible solution I've come across would be creating text fields rather than just displaying a list. That would mean I wouldn't actually be associating Types with Manufacturers until the commit.
    David K.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is no real issue populating two objects (as defined by two models) from a single form.

    You can use belongs_to and has_many relationship to simplify some of the coding in the area of how the components relate to one another. I'd recommend you have a look through the documentation for ActiveRecord::Associations::ClassMethods in the rails api site.

    Ensuring that models are behaving correctly together can be further monitored and/or enforced via ActiveRecord callbacks.

    The main problem I would envisage, is if you are using Ajax forms to partly submit data. Maintaining data over multiple submissions is relatively difficult without submitting at least some data to the database. In most systems, there needs to be an object to hold the data between each submission. You could use the session data, but that may be problematic.

    Personally, I think if I were building up these object over a number of submissions, I'd add a status field to the manufacturer object, which could be set to incomplete until the whole build process was complete and then build a process that would regularly clean up any incomplete objects.

    However, if you are just using Ajax to build the form, and that once the form was built and the data entered, the form was submitted in one action, I don't think you have a problem.

  4. #4
    SitePoint Enthusiast davidklaw's Avatar
    Join Date
    Jul 2004
    Location
    Detroit
    Posts
    57
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ReggieB, thanks for the input.

    I decided to take another route and follow the "Complex Forms" example as seen on Railscasts.com. Rather than providing a field that would allow the user to enter something and click 'Add' I now use a 'Add' link to generate a new form field. There's no intermediate saving now.

    My original use case was someone needing to add multiple child models quickly and easily and they would do this by simply typing in the form field, hitting enter, typing the next child, enter, next, enter, etc. It was getting messy so now the user has to click a link to generate the next form field - which honestly isn't that big of a deal.
    David K.


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
  •