Hi, can you help with the below problem?
I've got two tables in my sqlite3 database: people, departments; their models are linked with (people)belongs_to(departments) and (departments)has_many(people). As for contents of these tables: People has employee name and department (all string) columns; Departments has only name column. I've created several departments through a Rails view, which works fine. However, I've got an error with creating people at views/people/new.html.erb.
The actual error message is: ActiveRecord::AssociationTypeMismatch in PeopleController#create Department(#70161194961320) expected, got String(#70161244615680)
On this view, I've got a few text_fields to collect employee's details, and a select to have him select his department to store on the People database. I'm retrieving the departments list from the database to form the select in my view as follows:
list_departments is a helper: def list_departments
There is no issue with retrieving the departments list from the database, as the department names show correctly in the select options on the html. I think the problem is something to do with a mismatch between this form and what People table expects.
Any insights will be appreciated.
I believe the problem is in your select element. You want it to look like this:
Note that the attribute you associate with the select element is #department_id, not #department. The value that the HTTP post will send to your controller is the ID of the selected department. The #department= method (added by the
belongs_to :department macro) expects a Department model object. On the other hand, the #department_id= method (also added by the
belongs_to :department macro) expects a Department database ID, which is what your HTTP post contains.
Hi Adam, Your note makes sense, but it is still not quite there (it's dumping NoMethodError at People#new)... I cycled around belongs_to, couldn't find anything wrong (including config/routes.rb). Below is the debugger log:
(rdb:2) Processing PeopleController#new (for 127.0.0.1 at 2009-09-24 21:13:16) [GET] Rendering template within layouts/people Rendering people/new Department Load (2.2ms) SELECT * FROM "departments" ActionView::TemplateError (undefined method `department_id' for #) on line #16 of app/views/people/new.html.erb: 13: 14:
app/views/people/new.html.erb:16 app/views/people/new.html.erb:3 app/controllers/people_controller.rb:30:in `new' /usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start' /usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /usr/lib/ruby/1.8/webrick/server.rb:95:in `start' /usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /usr/lib/ruby/1.8/webrick/server.rb:23:in `start' /usr/lib/ruby/1.8/webrick/server.rb:82:in `start' Rendered rescues/_trace (188.2ms) Rendered rescues/_request_and_response (4.7ms) Rendering rescues/layout (internal_server_error) /usr/lib/ruby/gems/1.8/gems/rails-2.3.4/lib/rails/rack/log_tailer.rb:19 response
I think, that problem is in your database asociation.
Try to make your association this way.
Make attribute 'department_id' in your people table which is type :integer. And attribute 'id' in your departments table which is type :integer.
I thing, that this help you make your association easier.
Robin, thanks, all I had to was to add a department_id (integer) to Person table.
I think lessons learned then are: 1. To access another model's data, you need to have a pointer storage, which will store a pointer to the data you are associating. (e.g, department_id is storing the address of one of the department.name's) 2. We can't get to another object's _id= method from within one object's view. (we could not get Department object's :department_id= / id= method from People object's view.)