Collection Select Question / Belongs_to

I have a quick question regarding a belongs_to association and a collection_select in a form.

Here’s the pertinent code - ## Modelclass TerminalTicket < ApplicationRecord belongs_to :owner, class_n -

When I submit the form I get an error - User(#726040) expected, got “1” which is an instance of String(#2500)

Heya @sfalconecollection_select allows you to choose a value for a specific field, and not a whole object. In this case this is the user’s ID. In order to record it properly in the TerminalTicket, this should then be owner_id instead of owner:

<%= form.collection_select :owner_id, User.all, :id, :email %>

As well, in your terminal_tickets_controller.rb file make sure the params you permit lists owner_id and not just owner:

  def terminal_ticket_params
    params.require(:terminal_ticket).permit(:owner_id, :whatever_else, :ticket_date, ...)

I appreciate the reply - I tried this and now I get an error on the form submission that the owner must exist when I submit it. The select box is populated properly and the id is passing properly.

I’m trying to set it so that I have a field called ‘owner’ that is an integer in the TerminalTicket model that is referencing the user table…

Cool – have created a quick video walkthrough using The Brick to build this out in hopes that it might approach what you’re after:

Sometimes when you play back a video hosted out on Github, it stops before the end, so if that happens then note where it stopped, refresh the page, and pick back up at that point :slight_smile:

Thanks - this is super helpful!!! Wasn’t familiar with Brick but checking it out now.

I’m grateful for your help. Thanks a bunch!