Ruby on Rails Thursday, September 26, 2019

Here is a fun one for everyone...   

I have been struggling developing a form to do the following:

I have a Class with Students in it, and every day the teachers have to fill out a daily detail sheet which looks like this...

Untitled picture.png


Thank you in advance for any thoughts on this one... 

So what I have done is create a data scheme like this

dd_sheet
  dd_details
    dd_detail_categories
      dd_detail_items
       dd_detail_subitems
    dd_detail_subcategories
      dd_detail_items
        dd_detail_subitems

The sheet has the classroom, date and teacher...

The detail has a student, note and then Categories, Subcategories, Items, and Subitems

The Categories, Subcategories, Items, and Subitems are predefined from another set of tables and built when they create a new daily detail sheet

Model wise this all works great, so the relationships are correct

So on the form is where I am having the issue

Base Form is on DailyDetail

Need to step through each Category and display items related to that category first (including subitems related to the items)
Then step through each Subcategory and display items related to that category first (including subitems related to the items) 

Each Item/Subitem can have a check, a value, and/or a note so there is a check as to whether that value is needed. Here is the Item Table definition. Subitem is the same
------------
    create_table :dd_detail_items do |t|
      t.integer :dd_detail_category_id, null: true
      t.integer :dd_detail_subcategory_id, null: true
      t.string :title, null: false, length: 100
      t.boolean :has_check, default: false
      t.boolean :has_value, default: false
      t.boolean :has_note, default: false
      t.boolean :item_check, default: false
      t.string :item_value, default: ''
      t.text :item_note, default: ''
      t.timestamps null: false
    end
-------------

This is driving me bonkers trying to figure out this nesting....  I know logically how it should work but can't figure out the rails syntax to do it correctly...

Here are my models

dd_sheet
------------
  has_many :dd_details, dependent: :destroy
  belongs_to :classroom

  after_commit :build_nested, on: :create

  private

  def build_nested()
    classroom.students.each do |student|
      detail = DdDetail.create!(dd_sheet_id: id, student_id: student.id)
      # For each category, build the Items and Subcategories
      DdCategory.all.each do |category|
        # If the Category is active, build it
        if category.is_active?
          cat = detail.dd_detail_categories.create!(title: category.title)
          # Now build any items directly related to this Category
          category.dd_items.each do |item|
            # If the item is active, build it
            if item.is_active?
              newitem = cat.dd_detail_items.create!(dd_detail_category_id: cat.id, title: item.title, has_check: item.has_check, has_value: item.has_value, has_note: item.has_note, item_check: item.item_check, item_value: item.item_value, item_note: item.item_note)
              # If the item is has SubItems, build them
              if item.dd_subitems.count > 0
                item.dd_subitems.each do |subitem|
                  newitem.dd_detail_subsubitems.create!(dd_detail_item_id: newitem.id, title: subitem.title, has_check: subitem.has_check, has_value: subitem.has_value, has_note: subitem.has_note, item_check: subitem.item_check, item_value: subitem.item_value, item_note: subitem.item_note)
                end
              end
            end
          end
          category.dd_subcategories.each do |subcategory|
            # If the Subcategory is active, build it
            if subcategory.is_active?
              subcat = cat.dd_detail_subcategories.create!(title: subcategory.title)
              subcategory.dd_items.each do |item|
                # If the item is active, build it
                if item.is_active?
                  newitem = subcat.dd_detail_items.create!(dd_detail_category_id: subcat.id, title: item.title, has_check: item.has_check, has_value: item.has_value, has_note: item.has_note, item_check: item.item_check, item_value: item.item_value, item_note: item.item_note)
                  # If the item is has SubItems, build them
                  if item.dd_subitems.count > 0
                    item.dd_subitems.each do |subitem|
                      newitem.dd_detail_subitems.create!(dd_detail_item_id: newitem.id, title: subitem.title, has_check: subitem.has_check, has_value: subitem.has_value, has_note: subitem.has_note, item_check: subitem.item_check, item_value: subitem.item_value, item_note: subitem.item_note)
                    end
                  end
                end
              end
            end
          end
        end
      end  
    end
  end
------------


dd_detail
------------
  belongs_to :dd_sheet
  has_many :dd_detail_categories, dependent: :destroy
  belongs_to :student
------------


dd_detail_category
------------
  belongs_to :dd_detail
  has_many :dd_detail_subcategories, dependent: :destroy
  has_many :dd_detail_items, dependent: :destroy
  accepts_nested_attributes_for :dd_detail_items, allow_destroy: true
------------


dd_detail_subcategories
------------
  belongs_to :dd_detail_category
  has_many :dd_detail_items, dependent: :destroy
  accepts_nested_attributes_for :dd_detail_items, allow_destroy: true
------------


dd_detail_items
------------
  belongs_to :dd_detail_category
  belongs_to :dd_detail_subcategory
  has_many :dd_detail_subitems, dependent: :destroy
  accepts_nested_attributes_for :dd_detail_subitems, allow_destroy: true
------------


dd_detail_subitems
------------
  belongs_to :dd_detail_item
------------


--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/029fe73d-4938-4b56-9c4c-be5e011cc280%40googlegroups.com.

No comments:

Post a Comment