vendredi 1 mai 2015

Create checkboxes from only certain objects

At the moment I have units and categories as tables. Categories have a name, year, unit_id and exam_board_id. Units just have a name.

(Categories belong to units).

I am trying to create a checkbox of units that have a specific category.year and category.exam_board_id

At the moment I have tried to create two methods to try and pick out these units like this:

def ocr_as_units(unit)
    unit.categories.each do |category|
      if category.year = "AS" && category.exam_board_id == 1
        return true
      end
    end
  end

  def display_ocr_as_units(unit)
    # ocr_as_units = []
    if ocr_as_units(unit) == true
      # ocr_as_units << unit
      return unit.name
    end
  end

And in the form where I want to create the checkboxes I have the following code (in HAML):

- Unit.all.each do |unit|
   = display_ocr_as_units(unit)
   .category-label
     = label_tag dom_id(unit), unit.name
     = check_box_tag "test[unit_ids][]", unit.id, @test.unit_ids.include?(unit.id), id:dom_id(unit)

So far all this does is correctly display the units that are AS and OCR, but then underneath that creates the checkboxes for all the units. Is there a way to take the units that are returned with the display_ocr_as_units(unit) method and use those to create the checkboxes?

Thanks




Aucun commentaire:

Enregistrer un commentaire