1

Here's what I'm trying to do.

Create a view/model named Calls. It will list various pieces of data native to the table. The basic view (scaffold) I created does that via an instance variable in the controller. Now I want to bring a field from another table, in this case "units" into the view and allow the a field from units to be written to a field within calls upon submit.

call.rb:

class Call < ActiveRecord::Base
  attr_accessible :call_nature, :caller_address, :caller_name, :caller_phone, :incident_number
  validates :caller_name, :caller_phone, :caller_address, :presence => true
  has_many :units
end

units.rb:

class Units < ActiveRecord::Base
  attr_accessible :unit_name
  belongs_to :call
end

So in essence I want to be able to display my call fields within a view or form but also want to be able to access Calls.unit_name and display it for selection in my form and view.

I've been reading up on associations and it seems very straight forward but I seem to be having a problem.

If you need more information, please let me know.

Kevin Bedell
  • 13,254
  • 10
  • 78
  • 114
nulltek
  • 3,247
  • 9
  • 44
  • 94
  • 3
    You'll run into a pluralization issue - Rails assumes the class will be called `Unit` (i.e. singular), and the file will be `unit.rb`. The `has_many` call should be kept as `units`, though. – x1a4 May 13 '12 at 22:52
  • How would you suggest I rename my model? I figured it had something to do with pluralization but I'm still catching on to the singular/plural relationship. – nulltek May 13 '12 at 23:03
  • Rename it to `Unit`, and change the filename to `unit.rb`. – x1a4 May 13 '12 at 23:05
  • Should I also rename the view and controller to Unit? – nulltek May 13 '12 at 23:12
  • Yes. You should follow the rails conventions for pluralization and capitalization of model names. – Larry K May 13 '12 at 23:15
  • The controller should be `UnitsController`, i.e. with the plural. The views would be in `app/views/units`, also plural. – x1a4 May 13 '12 at 23:30
  • http://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails may be helpful on this subject. – x1a4 May 13 '12 at 23:31
  • http://itsignals.cascadia.com.au/?p=7 – Cristian Bica May 14 '12 at 06:08
  • Sticking to the Rails naming convention helped with my association problems. I redid the models and was able to link information together. – nulltek May 18 '12 at 01:26

0 Answers0