将两个用户关联到一个模型

发布于 2024-12-11 10:34:40 字数 2222 浏览 1 评论 0原文

我在让两个用户属于同一模型(票证)时遇到了一些问题。

在我正在构建的错误跟踪器中,票证是需要对项目完成的单个任务项。它附加了两个用户,Ticket.raised_by 和 Ticket.signed_to。创建用户

问题是我一直在 Tickets#create 操作上抛出 ActiveRecord::AssociationTypeMismatch 异常,并显示一条错误消息,看起来有点像

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

以下是 <代码>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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

剩一世无双 2024-12-18 10:34:40

我不确切知道您的身份验证是如何设置的,但我的猜测是您的模型无权访问 current_user,因此此代码因此失败:

before_create do
  self.raised_by = current_user
end

有多种方法可以访问 < code>current_user 在您的模型中,但存在一些风险(例如,它不是线程安全的)。有关示例,请参阅访问模型中的 current_user

不过,您可能希望在控制器中处理此问题,而不是使用挂钩来设置用户:

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

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, .....)
盛夏尉蓝 2024-12-18 10:34:40
User(#81485590) expected, got String(#68375030)

表明您错误地执行了:

Ticket.assigned_to = 123

而不是

Ticket.assigned_to_id = 123

代码中的某处。您可以在分配票证的地方发布视图/控制器代码吗?

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?

-小熊_ 2024-12-18 10:34:40

也许 current_user 是一个字符串?

它是如何进入模型的?用户会话是控制器的业务。

Maybe current_user is a string?

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

十年九夏 2024-12-18 10:34:40

在您的表单上,您是否有一个“分配给”字段(可能是一个选择框),其中每个用户的名称作为每个值?
如果是这样,当您在控制器中 update_attributes 时,它将尝试将该字段的字符串值放入分配的_to 关联中,这会导致错误
已经描述过。

将该字段重命名为“assigned_to_id”,并确保该字段的值是每个用户的 ID,而不是他们的姓名。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文