0

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
3
  • 1
    Try this: treatment.category.try(:typ) If category is not nil, it will display its attribute :typ Commented Jun 16, 2013 at 17:35
  • Do you know a site where .try is explaind? Somehow i never came in contact with it it? Commented Jun 16, 2013 at 17:41
  • 1
    You can find documentation here: apidock.com/rails/Object/try Commented Jun 16, 2013 at 17:45

3 Answers 3

1

it works with

  <td><%= treatment.category && treatment.category.typ %></td>

because category is nil for some treatment objects. If treatments need to have a category I would put a validation on the model level as well as a foreign key restrction on the database.

  class Treatment
    validates_presence_of :treatment
  end

and then in a migration

  remove_column :treatments, :category_id
  add_column :treatments, :category_id, :null => false

this will ensure referential integrity in your database. if the relationship is not required then ignore this. You can also make your code 1 method call by using .try

 <td><%= treatment.category.try(:typ)%></td>
Sign up to request clarification or add additional context in comments.

1 Comment

Because you mentioned validation, i have another question, that refers on validation. Maybe you can help me? stackoverflow.com/questions/17136314/…
1

Ok somehow it works with

<td><%= treatment.category && treatment.category.typ %></td>,

maybe someone knows why this works?

Comments

1

You use treatment.category.typ.

You also need @patient = Patient.where(:id => params[:id]).includes(:treatment => [:category]).first in your controller.

9 Comments

didnt worked, i get the error undefined method `typ' for nil:NilClass
You should include category in your query.
how? Sorry im new to rails!
@EmSta That queries the category and displays the type. But it is performing lazy loading, so if you want better performance, you should go with the query string above.
No, I mean you should preload your categories like this @patient = Patient.where(:id => params[:id]).includes(:treatment => [:category]).first
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.