0

I am building an application for test case management system. I have testcases and testruns. I am joining these 2 tables through assocation model called testresults.

class Testcase < ActiveRecord::Base
  has_many :testresults
  has_many :testruns, :through => :testresults

class Testrun < ActiveRecord::Base
  has_many :testresults
  has_many :testcases, :through => :testresults

class Testresult < ActiveRecord::Base
  belongs_to :testrun
  belongs_to :testcase

There is a fixed set of testcases already existing. Everytime I create a new testrun, I want to select a number of existing testcases and add to the new testrun.

I am not sure about the following things. If you could please give me some pointers about what approach should I take and what methods/functions/helpers should I use, I will be grateful. I have wasted more than a day on it going through different posts but not able to see things clearly.

  1. View - How to populate the existing testcases with checkboxes in front of them. Do I use options_from_collection_for_select? But how do i get checkboxes
  2. Do I use testrun model or the assocation model i.e. testresult. I am confused about where to show the added testcases - on the testrun page or testresult page.
  3. Do we create views, and use controllers for the assocaition model as well?
4

1 回答 1

0

You can: 1. You can use checkboxes (with check_box_tag) or select with multiple options. 2. You can pass the checkboxes in a different hash in the form 3. Get the data of the checkboxes, loop through the data and create the Testresult

The best way is using a has_and_belongs_to_many association, check this video from RailsCasts http://railscasts.com/episodes/17-habtm-checkboxes it is a little old but it works, and if you want a newer video check this out http://railscasts.com/episodes/17-habtm-checkboxes-revised

You should also check http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

于 2013-03-31T03:15:18.727 回答