samedi 14 mai 2016

Rails attributes not updating

I'm building an invitation system where a user can either invite a friend, setting their user.id to the sender field in the database, or request an invitation, which should set the sender to '0' (an integer field).

User model

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

Invitation model

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

Invitations controller

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

The invitation is being created (I can see it in the database), but the sender isn't being set. This is the output form the 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)

I'm stumped because #1, I'm not creating an arbitrary attribute "sender_id". The applicable column in the database is "sender", and #2, the POST isn't setting the sender id, which should either be "0" or the id of the current_user.

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire