Issues with triple nesting #68

Closed
yuletide opened this Issue May 25, 2012 · 5 comments

Projects

None yet

3 participants

@yuletide

Hey, love the library, but having some issues with nesting on this project
Don't think it's necessarily an issue with the library, but my head is spinning so thought I'd post in case you had any ideas!

Here's the hierarchy:

Poll (form) has_many Questions (form) has_many Options (Form) has_one Follow-up, class: Question (Form) has_many Options (Form)

UI renders fine, but issues arise editing objects with followups, or creating items with follow-up-options (the two deepest levels of nesting)

1- The most serious issue, When creating a poll: Unable to create more than one follow-up option. Everything looks fine in the UI, but only one follow-up-option gets posted and saved

When attempting to create a poll with the following structure

    Question, text: "First question" type "MULTI" 
        option, label: apples, value: a
        option, label: bananas, value: b
            Follow-up question, text: "Are you sure?", type: "YN"
                option: yes
                option: no

This is what gets posted

    Started POST "/polls" for 127.0.0.1 at 2012-05-24 18:42:07 -0700
    Processing by PollsController#create as HTML
    Parameters: {
       "utf8"   =>"✓",
       "authenticity_token"   =>"geW3nvwDaEftZyJUaAkmb69iD0aJJe/WT1/ynV+pl+M=",
       "poll"   =>   {
          "questions_attributes"      =>      {
             "1337909867131"         =>         {
                "text"            =>"First question",
                "_destroy"            =>"",
                "question_type"            =>"MULTI",
                "options_attributes"            =>            {
                   "1337909878928"               =>               {
                      "text"                  =>"apples",
                      "_destroy"                  =>"",
                      "value"                  =>"a"
                   },
                   "1337909884148"               =>               {
                      "text"                  =>"bananas",
                      "_destroy"                  =>"",
                      "value"                  =>"b",
                      "follow_up_attributes"                  =>                  {
                         "text"                     =>"Are you sure?",
                         "question_type"                     =>"YN",
                         "_destroy"                     =>"",
                         "options_attributes"                     =>                     {
                            "1337909884148"                        =>                        {
                               "text"                           =>"no",
                               "value"                           =>"n",
                               "_destroy"                           =>""
                            }
                         }
                      }
                   }
                }
             }
          },
          ... snip...
    }     

2- When editing: Can't edit any poll that has follow-ups. When attempting to edit, it seems follow-ups are being deleted. In the below log, questions_all grabs all the questions for a poll, including for any followups

Check this out

  1.9.3p125 :187 > Poll.find(57).questions_all
   => [#<Question id: 72, text: "where?", poll_id: 57, question_type: "OPEN", created_at: "2012-05-24 22:51:15", updated_at: "2012-05-24 22:51:15", sequence: nil, parent_option_id: nil>, #<Question id: 70, text: "yes or no?", poll_id: 57, question_type: "OPEN", created_at: "2012-05-24 22:51:15", updated_at: "2012-05-25 01:23:36", sequence: nil, parent_option_id: nil>, #<Question id: 73, text: "aaaaa", poll_id: nil, question_type: "OPEN", created_at: "2012-05-25 01:23:36", updated_at: "2012-05-25 01:23:36", sequence: nil, parent_option_id: 55>] 

  1.9.3p125 :188 > Poll.find(57).options_all
   => [#<Option id: 55, text: "yes", question_id: 70, value: "yes", follow_up_id: nil, created_at: "2012-05-24 22:51:15", updated_at: "2012-05-24 22:51:15">, #<Option id: 56, text: "no", question_id: 70, value: "no", follow_up_id: nil, created_at: "2012-05-24 22:51:15", updated_at: "2012-05-24 22:51:15">] 

  polls/57/edit with no changes, just hit "update"

    ActiveRecord::RecordNotFound in PollsController#update

    Couldn't find Question with ID=73 for Option with ID=55

  then...

  1.9.3p125 :189 > reload!
  Reloading...
   => true 
  1.9.3p125 :190 > Poll.find(57).questions_all
   => [#<Question id: 72, text: "where?", poll_id: 57, question_type: "OPEN", created_at: "2012-05-24 22:51:15", updated_at: "2012-05-24 22:51:15", sequence: nil, parent_option_id: nil>, #<Question id: 70, text: "yes or no?", poll_id: 57, question_type: "OPEN", created_at: "2012-05-24 22:51:15", updated_at: "2012-05-25 01:23:36", sequence: nil, parent_option_id: nil>] 
  1.9.3p125 :191 > Question.find(73)
    Question Load (0.7ms)  SELECT "questions".* FROM "questions" WHERE "questions"."id" = $1 LIMIT 1  [["id", 73]]
  ActiveRecord::RecordNotFound: Couldn't find Question with id=73

I'm pretty much out of ideas, my next step was to create a separate follow-ups table (or try using Single Table Inheritance to emulate it) and see if that fixed anything...
Thanks again!

@yuletide

Some updates.

For issue 2. If I remove :dependent => :destroy from the Option has_one follow-up relationship I get this error.

    ActiveRecord::RecordNotSaved in PollsController#edit

    Failed to remove the existing associated follow_up. The record failed to save when after its foreign key was set to nil.
    Rails.root: /Users/ayule/code/txtyourcity_rails

    Application Trace | Framework Trace | Full Trace
    app/views/polls/_option_fields.html.erb:11:in `_app_views_polls__option_fields_html_erb___3590358424531303420_70248388674980'

(_option_fields.html.erb:11 is "link_to_add_association :follow_up")

Seems the issue is due to follow_up being a has_one relationship. Upon editing, link_to_add_association tries to destroy the follow_up (if able).

Everything seems to work fine once I switch it to a has_many. So I guess this becomes a feature request for support of has_one relationships!

Issue 1 is still unresolved... if it would be helpful I could spin up a demo instance of the app and send over the link!

@nathanvda
Owner

Sorry I didn't react earlier. Where you able to solve this problem? I have a demo-project which show-cases multiple relation-types, and nested different levels deep here which might help you as well.

@nathanvda
Owner

Closed due to inactivity.

@nathanvda nathanvda closed this Oct 12, 2012
@mdkrog
mdkrog commented Sep 18, 2014

Hi Nathan,

I am encountering the same error as yuletide describes. I checked out your example project, but there is no instance in this project of a has_one relationship. Would you be able to shed some light on this.

Thanks,
Matt

@nathanvda
Owner

Hi @mdkrog, could you open a new issue, and show me some code? I am in the process of updating the example project as well. There was a wiki page, demonstrating/explaining the different relations, but somehow it was deleted, I will try to get it back asap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment