I am using the 'state_machine' gem. Once I create a new UserFriendship, the user_id and friend_id do not get saved. I even tried creating manually the friendship in the console by passing the user_id and friend_id directly but still these two variables do not get saved even though the friendship gets created in the database, the friend and user are not saved . I do not know what 's wrong as it seems to me I am doing everything right.
The 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
User Model
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
The UserFrienships Controller
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
The User Friendship new view
<% 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%>
The Migrations
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
Last Migration
class AddStateToUserFriendships < ActiveRecord::Migration
def change
add_column :user_friendships, :state, :string
add_index :user_friendships, :state
end
end
UserFriendship table from latest 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"