tubbo
tubbo

Reputation: 608

associating two users to a model

I'm having a bit of an issue getting two Users to belong_to the same model (Ticket).

In the bug tracker I'm building, a Ticket is a single task item that needs to be done to a Project. It has two Users attached to it, Ticket.raised_by and Ticket.assigned_to. The creating user

The problem is I consistently get an ActiveRecord::AssociationTypeMismatch exception thrown on the Tickets#create action, with an error message that looks kinda like

User(#81485590) expected, got String(#68375030)

Here's the code for Ticket.rb:

    # Table name: tickets
    #
    #  id             :integer         not null, primary key
    #  title          :string(255)
    #  description    :text
    #  status         :string(255)     default("open")
    #  priority       :boolean         default(FALSE)
    #  project_id     :integer
    #  raised_by_id   :integer         default(0)
    #  assigned_to_id :integer         default(0)
    #  created_at     :datetime
    #  updated_at     :datetime

    class Ticket < ActiveRecord::Base
      validates :title,   :presence => true
      validates :status,  :presence => true
      belongs_to :project
      belongs_to :raised_by,    :foreign_key => 'raised_by_id',   :class_name => 'User'
      belongs_to :assigned_to,  :foreign_key => 'assigned_to_id', :class_name => 'User'

      before_create do
        self.raised_by = current_user
      end

      state_machine :status, :initial => 'open' do
        # when a ticket is "claimed" by a developer
        event :work do
          transition any => :working_on
        end

        # posted the solved ticket to the dev server (or VM)
        event :post do
          transition any => :posted
        end

        # ticket is under review by another developer
        event :review do
          transition any => :reviewed
        end

        # review is completed and confirmed. only reviewed or flagged tasks can be completed.
        event :complete do
          transition [:review, :flagged] => :completed
        end

        # occurs when the project is completed
        event :close do
          transition any => :closed
        end

        # Special treatment for administrators. Sends them a Message detailing the flagged issue.
        event :flagged do
          transition any => :flagged
        end
      end
    end

Upvotes: 0

Views: 144

Answers (4)

Pavling
Pavling

Reputation: 3963

On your form, do you by any chance have an "assigned to" field (probably a select box) with the name of each user as each value? If so, when you update_attributes in your controller, it will try to put the string value of that field into the assigned_to association, which breaks with the error you ve described.

Rename the field to "assigned_to_id" and make sure the value of the field is the id of each respective user, rather than their name.

Upvotes: 0

Leonid Shevtsov
Leonid Shevtsov

Reputation: 14179

Maybe current_user is a string?

How did it even get into the model? User sessions are controller business.

Upvotes: 0

Alex Peattie
Alex Peattie

Reputation: 27647

User(#81485590) expected, got String(#68375030)

suggest that you're incorrectly doing:

Ticket.assigned_to = 123

rather than

Ticket.assigned_to_id = 123

somewhere in your code. Can you post the view/controller code where you assigning the ticket?

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124419

I don't know exactly how your authentication is set up, but my guess is that your model does not have access to current_user, so this code is failing because of that:

before_create do
  self.raised_by = current_user
end

There are ways to access the current_user in your model, but there's some risk involved (for example, it's not thread safe). See Access current_user in model for an example.

Rather than use a hook to set the user, though, you may want to handle this in the controller instead:

@ticket = Ticket.create(:raised_by => current_user, .....)

Upvotes: 2

Related Questions