In my projekt i have two models, an "Treatment"-Model and a "Category"-Model
class Category < ActiveRecord::Base
attr_accessible :typ
has_many :treatments
end
class Treatment < ActiveRecord::Base
belongs_to :patient
belongs_to :category
attr_accessible :content, :day, :typ, :category_typ
end
So in my treatment form the user can also choose the category:
<div class="field">
<%= f.label :category_id %><br />
<%= f.collection_select :category_id, Category.find(:all), :id, :typ %>
</div>
My problem is that later i can display the category_id but i really dont know how i can display the catogory typ:
<% @patient.treatments.each do |treatment| %>
<tr>
<td><%= treatment.category_id %></td>
<td><%= treatment.content %></td>
<td><%= treatment.day %></td>
</tr>
<% end %>
I tried category_typ, but didnt worked! Im beginner in rails and i hope somebody can help me! Thanks!
def show
@patient = Patient.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @patient }
end
end
treatment.category.try(:typ)If category is not nil, it will display its attribute:typ