我的 UserFriendship 创建操作不保存 user_id 和 friend_id
My UserFriendship create action does not save user_id and friend_id
我正在使用 'state_machine' gem。
创建新的 UserFriendship 后,user_id 和 friend_id 不会保存。我什至尝试通过直接传递 user_id 和 friend_id 在控制台中手动创建友谊,但是即使在数据库中创建了友谊,朋友和用户也没有保存这两个变量保存。我不知道哪里出了问题,因为在我看来我做的一切都是对的。
用户友谊模型:
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
UserFriendship table 来自最新 schema.rb
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"
您应该从 user_friendship
模型中的 attr_accessor
中删除 friend_id
和 user_id
,以便将这些值保存在 DB
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
attr_accessor :user, :friend, :state
-----
end
我正在使用 'state_machine' gem。 创建新的 UserFriendship 后,user_id 和 friend_id 不会保存。我什至尝试通过直接传递 user_id 和 friend_id 在控制台中手动创建友谊,但是即使在数据库中创建了友谊,朋友和用户也没有保存这两个变量保存。我不知道哪里出了问题,因为在我看来我做的一切都是对的。
用户友谊模型:
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
UserFriendship table 来自最新 schema.rb
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"
您应该从 user_friendship
模型中的 attr_accessor
中删除 friend_id
和 user_id
,以便将这些值保存在 DB
class UserFriendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
attr_accessor :user, :friend, :state
-----
end