ruby on rails - Using checkboxes with parameters from a many-to-many link table -


i have following models

class outbreak < activerecord::base     has_many :risks     has_many :factors, :through => :risks end  class risk < activerecord::base     belongs_to :outbreak     belongs_to :factor end  class factor < activerecord::base     has_many :risks     has_many :outbreaks, :through => :risks end  risk table id : integer outbreak_id : integer factor_id : integer details : text 

in view/outbreak/edit

   <div id="factor_div">     <% factor in factor.find(:all, :conditions => {:outbreak_type => @outbreak.outbreak_type}) %>         <div>         <%= check_box_tag "outbreak[factor_ids][]", factor.id , @outbreak.factors.include?(factor) %>         <%= factor.name %>         <%= text_field_tag "risk[details][]", @outbreak.risks.each{ |risk| if risk.factor_id == factor.id; risk} %>         </div>      <% end %>    </div> 

i'm trying edit risk model details attribute (and populate correct text_field @outbreak.risks array). possible using approach of iterating through factors (which table of static variables - have occassionally altered end-users) , checking whether or not each outbreak.risk has factor_id or going wrong way?

(can't think - friday afternoon , sunny out.... , theres beer garden down road ^^).

first, correct association , update results.

class factor < activerecord::base   has_many :risks   has_many :outbreaks, :through => :risks end 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -