Find items with belongs_to associations in Rails?

Posted by dannymcc on Stack Overflow See other posts from Stack Overflow or by dannymcc
Published on 2010-06-08T14:24:41Z Indexed on 2010/06/08 14:52 UTC
Read the original article Hit count: 329

Filed under:
|
|
|

Hi Everyone,

I have a model called Kase each "Case" is assigned to a contact person via the following code:

class Kase < ActiveRecord::Base
  validates_presence_of :jobno
  has_many :notes, :order => "created_at DESC"

  belongs_to :company # foreign key: company_id
  belongs_to :person # foreign key in join table
  belongs_to :surveyor,
             :class_name => "Company",
             :foreign_key => "appointedsurveyor_id"
  belongs_to :surveyorperson,
             :class_name => "Person",
             :foreign_key => "surveyorperson_id"

I was wondering if it is possible to list on the contacts page all of the kases that that person is associated with.

I assume I need to use the find command within the Person model? Maybe something like the following?

def index
@kases = Person.Kase.find(:person_id)

or am I completely misunderstanding everything again?

Thanks,

Danny

EDIT:

If I use:

@kases= @person.kases

I can successfully do the following:

<% if @person.kases.empty? %>
  No Cases Found
<% end %>

<% if @person.kases %>
 This person has a case assigned to them
<% end %>

but how do I output the "jobref" field from the kase table for each record found?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about model