views:

49

answers:

1

Hi Everyone,

I have a select list drop down in my Rails application, in the form for creating a new record.

<li>Surveyor Name<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

This is so the user can choose from a list of Surveyor's names which one they want to associate with the Case they are creating.

What I would like to do is have the company name of the Surveyor listed next to their name in brackets.

At the moment the code looks like this:

<li>Appointed Company<span><%= f.select :appointedsurveyor_id, Company.all.collect {|m| [m.companyname, m.id]}, {:selected => (@kase.appointedsurveyor_id rescue "")} %></span></li>
<li>Appointed Surveyor<span><%= f.select :surveyorperson_id, Person.all.collect { |x| [x.personname, x.id]}, {:selected => (@kase.surveyorperson_id rescue "")} %></span></li>

Is it possible to have the company that a surveyor works for outputting after their name?

The associations are as follows:

class Company < ActiveRecord::Base
  has_many :kases
  has_many :people

class Person < ActiveRecord::Base
  has_many :kases # foreign key in join table
  belongs_to :company

I have also changed to output of the company records to their names rather than their ID numbers'.

def to_s; companyname; end

Thanks in advance!

Thanks,

Danny

+1  A: 

You can add a method to your Person model that returns a string containing the persons name and the Company he/she is working for:

def name_and_company
  return "#{name} [#{company.name}]"
end

If you do not want to have this in your model (because this is view related code), you can, instead of just calling

x.personname

inside the Person.all.collect call, you can use something like

"#{name} [#{company.name}]"
Calavera
Once I have added the def name_and_company, how do I call that method from within the select code in the view?
dannymcc
instead of calling x.personname, you call x.name_and_company
Calavera