Rails 属性未更新

Rails attributes not updating

我正在构建一个邀请系统,用户可以在其中邀请朋友,将他们的 user.id 设置为数据库中的 sender 字段,或者请求邀请,这应该设置 sender 到 '0'(整​​数字段)。

用户模型

...
has_many :invites, :class_name => 'Invitation', :foreign_key => 'sender' #sent_invitations
belongs_to :invitation
...

邀请模型

belongs_to :sender, :class_name => 'User'
has_one :recipient, :class_name => 'User'

邀请控制器

def create
    @invitation = Invitation.new(invitation_params)
    if current_user?
      @invitation.sender = current_user
      if @invitation.save
        redirect_to invitations_url, notice: 'Thank you. Your invitation has been sent.'
      else
        render action: "new"
      end
    else
      @invitation.sender = 0
      if @invitation.save
        redirect_to invitations_url, notice: 'Thank you. You request is being processed..'
      else
        render action: "new"
      end
    end
  end

正在创建邀请(我可以在数据库中看到它),但尚未设置发件人。这是 dev_log:

的输出形式
Started POST "/invitations" for 127.0.0.1 at 2016-05-14 17:49:54 -0600
Processing by InvitationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"aCasBmkfw0m1T/EwuBUlXTA/z+REEWo3Hpv2HpB9w6s=", "invitation"=>{"name"=>"john", "surname"=>"public", "recipient_email"=>"jp@sasdf.com"}, "commit"=>"Create Invitation"}
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."auth_token" = 'i__MG0iqyoIND68k6qJmvw' LIMIT 1
DEPRECATION WARNING: You're trying to create an attribute `sender_id'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from create at C:/Sites/template/app/controllers/invitations_controller.rb:38)
   (0.0ms)  begin transaction
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 'jp@sasdf.com' LIMIT 1
  SQL (1.0ms)  INSERT INTO "invitations" ("created_at", "invite_token", "name", "recipient_email", "sender", "sent_at", "surname", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?)  [["created_at", Sat, 14 May 2016 23:49:55 UTC +00:00], ["invite_token", "m_1zd0UxW3W1JqoDdp1EMA"], ["name", "john"], ["recipient_email", "jp@sasdf.com"], ["sender", nil], ["sent_at", nil], ["surname", "public"], ["updated_at", Sat, 14 May 2016 23:49:55 UTC +00:00]]
   (0.0ms)  UPDATE "users" SET "invites_avail" = 4, "updated_at" = '2016-05-14 23:49:55.161966' WHERE "users"."id" = 2
   (70.0ms)  commit transaction
Redirected to http://localhost:3000/invitations
Completed 302 Found in 193.0ms (ActiveRecord: 76.0ms)

我很困惑,因为 #1,我没有创建任意属性 "sender_id"。数据库中的适用列是 "sender",#2,POST 没有设置 sender id,它应该是“0”或者 [=36] 的 id =].

我做错了什么?

我找不到我的问题的答案,所以我改用了它。

因为当邀请从 current_user 发送时,我能够让 invitation.sender 保存,其余的只是在数据库中设置任何内容 (NULL) ,我更改了 invitations_controller.rb 中的查询参数以查找 @invitation.sender = nil 所有公司发送的邀请。

我会留下这个 until/unless 有人发布了更好的回复。干杯!