将两个用户关联到一个模型
我在让两个用户属于同一模型(票证)时遇到了一些问题。
在我正在构建的错误跟踪器中,票证是需要对项目完成的单个任务项。它附加了两个用户,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确切知道您的身份验证是如何设置的,但我的猜测是您的模型无权访问
current_user
,因此此代码因此失败:有多种方法可以访问 < code>current_user 在您的模型中,但存在一些风险(例如,它不是线程安全的)。有关示例,请参阅访问模型中的 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: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:
表明您错误地执行了:
而不是
代码中的某处。您可以在分配票证的地方发布视图/控制器代码吗?
suggest that you're incorrectly doing:
rather than
somewhere in your code. Can you post the view/controller code where you assigning the ticket?
也许
current_user
是一个字符串?它是如何进入模型的?用户会话是控制器的业务。
Maybe
current_user
is a string?How did it even get into the model? User sessions are controller business.
在您的表单上,您是否有一个“分配给”字段(可能是一个选择框),其中每个用户的名称作为每个值?
如果是这样,当您在控制器中 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.