0

一点背景。我正在尝试在创建时预填充我的 Document 模型的属性。为了做到这一点,我使用了一种基本形式的“current_user”助手。

但是,当我尝试设置时@document.creator = current_user.id出现ActiveRecord::AssociationTypeMismatch ... User expected, got Fixnum错误。

我承认我不知道为什么。有人能够澄清究竟是什么不正确吗?

相关的模型和控制器是:

class User < ActiveRecord::Base
  include Authority::UserAbilities
  attr_accessible :email, :name
  has_many :authorizations
end

class Document < ActiveRecord::Base
  has_one :creator, class_name: "User", foreign_key: "id", primary_key: "creator"
  attr_accessible :document_subject, document_keywords
  def as_json(options={})
    {
        :document_subject => document_subject,
        :document_keywords => document_keywords,
    }
  end
end

class ApplicationController < ActionController::Base
  protect_from_forgery
  private
  def current_user
    @current_user ||= User.find(session[:user_id]) if session[:user_id]
  end
  helper_method :current_user
end

class DocumentsController < ApplicationController

def create
    @document = Document.new(params[:document])
    @document.creator = current_user.id
    @document.save

    respond_to do |format|
      if @document.save
        format.json { render json: @document, status: :created, location: @document }
      else
        format.json { render json: @document.errors, status: :unprocessable_entity }
      end
    end
  end

和数据库模式(写入数据库)

PRAGMA table_info(users);
[,
 {
  "cid": 0,
  "name": "id",
  "type": "INTEGER",
  "notnull": 1,
  "dflt_value": null,
  "pk": 1
 },
 {
  "cid": 1,
  "name": "name",
  "type": "varchar(255)",
  "notnull": 0,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 2,
  "name": "email",
  "type": "varchar(255)",
  "notnull": 0,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 3,
  "name": "created_at",
  "type": "datetime",
  "notnull": 1,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 4,
  "name": "updated_at",
  "type": "datetime",
  "notnull": 1,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 5,
  "name": "active",
  "type": "boolean",
  "notnull": 1,
  "dflt_value": 'f',
  "pk": 0
 }
]

PRAGMA table_info(Documents);

[,
 {
  "cid": 0,
  "name": "id",
  "type": "INTEGER",
  "notnull": 1,
  "dflt_value": null,
  "pk": 1
 },
 {
  "cid": 1,
  "name": "created_at",
  "type": "datetime",
  "notnull": 1,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 2,
  "name": "updated_at",
  "type": "datetime",
  "notnull": 1,
  "dflt_value": null,
  "pk": 0
 },
 {
  "cid": 3,
  "name": "creator",
  "type": "integer",
  "notnull": 0,
  "dflt_value": null,
  "pk": 0
 },
{
  "cid": 4,
  "name": "document_subject",
  "type": "varchar(255)",
  "notnull": 0,
  "dflt_value": null,
  "pk": 0
 },
{
  "cid": 5,
  "name": "document_keywords",
  "type": "varchar(255)",
  "notnull": 0,
  "dflt_value": null,
  "pk": 0
 }
]
4

2 回答 2

1

它期待 Active Record 对象本身而不是 ID。你只需要做@document.creator = current_user

于 2013-10-26T21:18:26.480 回答
0

Beerlington 的回复按描述工作,但仍然不是我想要实现的理想解决方案。

我最终改变了策略:我没有向文档模型添加创建者字段,而是使用创建文档时授予的rolify角色,使用:

@document.save
current_user.add_role :creator, @document
于 2013-10-29T09:00:02.610 回答