1

Using the Rails 4.1.4 framework. Having a problem adding a 'friend_id' to the user_friendships table below... when the program runs in rails, I am redirected to the root (as expected) and given the success message that the friendship was created. However, when I crack open the database GUI, I see that only a user_id is saved into the user_id column, and the friend_id column is always left 'nil', no matter who friends who. I've searched high and low - I know it must be something simple and it's driving me crazy. Any help is much appreciated!

Can anyone see the mistake I may have made in the code that would prevent this from being saved? Could it be a missing strong parameters issue for friend_id, and if so, how do I go about correcting it?

The Models:

class User < ActiveRecord::Base
  has_many :user_friendships
  has_many :friends, through: :user_friendships


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

The Controller:

class UserFriendshipsController < ApplicationController
    before_filter :authenticate_user!, only: [:new]

    def new
        if params[:friend_id]
            @friend = User.where(profile_name: params[:friend_id]).first
            raise ActiveRecord::RecordNotFound if @friend.nil?
            @user_friendship = current_user.user_friendships.new(friend: @friend)
        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.where(profile_name: params[:user_friendship][:friend_id]).first
            @user_friendship = current_user.user_friendships.new(friend: @friend)
            @user_friendship.save
            redirect_to root_path
            flash[:success] = "You are now friends." #{@friend.full_name}
        else
            flash[:error] = 'Friend required.'
            redirect_to root_path
        end
    end

user_friendships view files --new.html.erb

<% if @friend %>

    <h1> <%= @friend.full_name %> </h1>

    <p> Do you really want to become friends with <%= @friend.full_name %>?</p>

    <%= form_for @user_friendship, method: :post do |f| %>
        <div class="form form-actions">
            <%= f.hidden_field :friend_id, value: @friend.profile_name %>
            <%= submit_tag "Yes, Add Friend", class: 'btn btn-primary' %>
            <%= link_to "Cancel", profile_path(@friend), class: 'btn' %>
        </div>

    <% end %>

<% end %>

-- submission button for form above on profile page

<div class="page-header">
    <h1> <%= @user.full_name%> </h1>
    <%= link_to "Add Friend", new_user_friendship_path(friend_id: @user), class:'btn'%>
</div>
  • Maybe you should use `build` instead of `new` for building association model: `current_user.user_friendships.build(friend_id: @friend.id)` (added `id` to be sure). – zishe Aug 23 '14 at 04:48
  • Thanks for the reply zishe. Tried the code you provided, but ActiveController throws a NoMethodError (undefined method 'id'). Calling build without @friend.id will work, but I have the same problem as before, it won't add the friend_id, haha. I know I'm missing something simple! – Tanner Brandt Aug 23 '14 at 04:59
  • Should note -- the tutorial I'm doing (teamtreehouse.com) is having me override the `to_param` to the user_name. So the friend_id that is being passed through is the user_name. Not sure if this causes a problem, but it seems to be working for them. – Tanner Brandt Aug 23 '14 at 05:03

1 Answers1

1

You have a problem with profile_name. I don't know what is it, but it should be an id. So, in controller change model search to:

@friend = User.find_by_id(params[:user_friendship][:user_id])

And in template <%= f.hidden_field :friend_id, value: @friend.id %>

This works for me

However, if you are using friendly_id gem, or another thing that force you to use profile_name instead of id, you should also use it in other places, like links:

new_user_friendship_path(friend_id: @user.profile_name)

Maybe this will help.

zishe
  • 10,665
  • 12
  • 64
  • 103
  • 1
    Thank you zishe!! That's exactly what was going on. The to_param override that I had included in my user model was returning the profile_name instead of the id from the table... and so it could not be written to the friend_id integer column. I cloned your git and it was incredibly helpful, I was able to figure it out. Instead of using the to_param method, in the future I think I'll create a branch and use the friendly_id gem and follow the documentation carefully. Thanks again zishe!! – Tanner Brandt Aug 23 '14 at 22:37
  • Great! Please, accept my answer then. There is [a daw in the left side](http://stackoverflow.com/tour) – zishe Aug 24 '14 at 05:15
  • Any idea on this one question ? http://stackoverflow.com/questions/33459295/my-userfriendship-create-action-does-not-save-user-id-and-friend-id – codigomonstruo Nov 01 '15 at 05:25