Forum: Ruby on Rails Display options from HABTM

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
724a9d1d0244e08cb4be518c88648d9f?d=identicon&s=25 Andy Park (prot)
on 2006-01-21 04:26
I have a large certifications table that I would like to have a little
more controll over, it is part of a HABTM relationship.

-----------------------------------
db's (simplified)

>certifications
id
course

>employees
id
department_id
type_id
name

>departments
id
name

>types
id
kind

>certifications_employees
certification_id
employee_id

-----------------------------------------

Models

class Employee < ActiveRecord::Base
    belongs_to :type
    belongs_to :department
    has_and_belongs_to_many :certifications
end

class Department < ActiveRecord::Base
    has_many :employees
end

class Type < ActiveRecord::Base
    has_many :employees
end

------------------------------------------

Using the following code in my employees view _form.rhtml produces
output from my certifications table, it works. The problem is, its a
large table and this will potetially produce hundreds of checkboxes.
---------------------------------------------------
<h1>Certification</h1>
<p>
<% for certification in @certifications %>
<input type="checkbox"
id="<%=certification.id%>"
name="certification_ids[]"
value="<%=certification.id%>"
<%if @employee.certifications.include?
certification%>checked="checked"<% end %>
><%=certification.course%> <br />
<% end %>
----------------------------------------------------

I think I really have two questions.

Is there a way, by way of a the employees_helper.rb to format the result
of <%=certification.course%> may be into 3 or 4 columns ?

And

How could I display just the records I wanted from the table ? For
example, I tried the following, which produced this erorr - undefined
method `apparatus_pump_testing'

<% for certification in @certifications %>
<input type="checkbox"
id="<%=certification.id%>"
name="certification_ids[]"
value="<%=certification.id%>" >
<%=certification.apparatus_pump_testing%>
<% end %>

Kindest regards and many thanks.
This topic is locked and can not be replied to.