我的UserFriendship创建操作不保存user_id和friend_id

时间:2015-11-01 04:44:58

标签: ruby-on-rails controller state-machine relationships

我正在使用' state_machine'宝石。 创建新的UserFriendship后,user_id和friend_id不会被保存。我甚至尝试通过直接传递user_id和friend_id来手动创建控制台中的友谊,但即使友谊在数据库中创建,朋友和用户也未保存,这两个变量仍然无法保存。我不知道在我看来我做错了什么是错的。

UserFriendship Model:

class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'

attr_accessor :user, :friend, :user_id, :friend_id, :state

    after_destroy :delete_mutual_friendship!


   state_machine :state, initial: :pending do

    after_transition on: :accept, do: [:send_acceptance_email, :accept_mutal_friendship!]

    state :requested
    event :accept do
    transition any => :accepted
    end
end


def self.request(user1, user2)
    transaction do
    friendship1 = UserFriendship.create!(user_id: user1.id, friend_id: user2.id, state: 'pending')
    friendship2 = UserFriendship.create!(user_id: user2.id, friend_id: user1.id, state: 'requested')
    #friendship1.send_request_email
    friendship1
end


end

def send_request_email
    UserNotifier.friend_requested(id).deliver
end

def send_acceptance_email
    UserNotifier.friend_request_accepted(id).deliver

end
def mutual_friendship
   self.class.where({user_id: friend_id, friend_id: user_id}).first 

end

def accept_mutal_friendship!

    mutual_friendship.update_attribute(:state, 'accepted')
end

def delete_mutual_friendship!
    mutual_friendship.delete
end

用户模型

class User < ActiveRecord::Base
 has_many :user_friendships
 has_many :friends, -> { where(user_friendships: { state: "accepted"}) },         through: :user_friendships
 has_many :pending_user_friendships, -> { where  state: "pending" }, class_name: 'UserFriendship', foreign_key: :user_id                                  
 has_many :pending_friends, through: :pending_user_friendships, source: :friend

UserFrienships控制器

class UserFriendshipsController < ApplicationController
before_filter :authenticate_user!

def accept
    @user_friendship = current_user.user_friendships.find(params[:id])
    if @user_friendship.accept!
        flash[:success] = "You are now friends with #{@user_friendship.friend.name}"
    else
        flash[:error] = "That friendship could not be accepted"
    end
    redirect_to user_friendships_path
end
def new
    if params[:friend_id]
        @friend = User.find(params[:friend_id])
        @user_friendship = current_user.user_friendships.new(friend: @friend)
        raise ActiveRecord::RecordNotFound if @friend.nil?

    else
        flash[:error] = "Friend Required"
    end

rescue ActiveRecord::RecordNotFound
    render file: 'public/404', status: :not_found
end 

def create

    if params[:user_friendship] && params[:user_friendship].has_key?(:friend_id)
        @friend = User.find_by(id: params[:user_friendship][:friend_id])
        @user_friendship = UserFriendship.request(current_user, @friend)


        if @user_friendship.new_record?
            flash[:error] = "There was a problem creating that friend request."

        else

            flash[:sucess] = "Friend request sent"

        end
        redirect_to current_user
    else
        flash[:error] = "Friend required"
        redirect_to root_path
end 
end

def index
    @user_friendships = current_user.user_friendships.all
end


def edit
    @user_friendship = current_user.user_friendships.find(params[:id])
    @friend = @user_friendship.friend
end

def destroy
    @user_friendship = current_user.user_friendships.find(params[:id])
    if @user_friendship.destroy
        flash[:success] = "Friendship destroyed"
       redirect_to user_friendships_path

end
end
end

用户友情新视图

<% if @friend %>

<h1><%= @friend.name %></h1>
<p> Do you really want to friend <%= @friend.name %>?</p>

<%= form_for @user_friendship, method: :post do |form| %>
<div class= "form form-actions" >

 <%= form.hidden_field :friend_id, value: @friend.id %>
 <%= submit_tag "Yes, Add Friend", class: 'btn btn_primary' %>
 <%= link_to "Cancel", user_path(@friend), class: 'btn' %>

</div>

<%end%>

<%end%>

迁移

class CreateUserFriendships < ActiveRecord::Migration
def change  
create_table :user_friendships do |t|
    t.integer :user_id 
    t.integer :friend_id 
    t.timestamps
 end
    add_index :user_friendships, [:user_id, :friend_id]
end
end

上次迁移

class AddStateToUserFriendships < ActiveRecord::Migration
 def change
  add_column :user_friendships, :state, :string
  add_index :user_friendships, :state
 end
end

来自最新schema.rb的UserFriendship表

 create_table "user_friendships", force: true do |t|
  t.integer  "user_id"
  t.integer  "friend_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.string   "state"
 end
 add_index "user_friendships", ["state"], name: "index_user_friendships_on_state"
 add_index "user_friendships", ["user_id", "friend_id"], name: "index_user_friendships_on_user_id_and_friend_id"

1 个答案:

答案 0 :(得分:1)

您应该从friend_id模型中的user_id移除attr_accessoruser_friendship,以便将这些值保存在 数据库

class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'

attr_accessor :user, :friend, :state
-----
end