John Smith
John Smith

Reputation: 6259

How to display text of category, has-one belongs to associaton

Ok i have two models:

class Treatment < ActiveRecord::Base
attr_accessible :category_id, :content, :date, :patient_id
has_one :category
end

class Category < ActiveRecord::Base
attr_accessible :text
has_many :treatments
end

In my application i tried now to display the text of the category instead of the id for each treatment. Among others i tried this:

<% @treatments.each do |f| %>
<%= f.content %>
<%= f.date %>
<%= f.category.try(:text) %>
<% end %>

But i get the error:

SQLite3::SQLException: no such column: categories.treatment_id

So how can i get the text of the category instead of the id? Thanks

Upvotes: 0

Views: 53

Answers (1)

PaReeOhNos
PaReeOhNos

Reputation: 4398

It looks as though you've got an issue with your model relationships. If you're after a standard one-to-many relationship then you need to change

has_one :category

to

belongs_to :category

in your Treatment model.

Upvotes: 3

Related Questions