views:

93

answers:

2

I'm creating a social network in Rails and I have a model like this:

create_table "friendships", :force => true do |t|
    t.integer  "user1_id"
    t.integer  "user2_id"
    t.boolean  "hasaccepted"
    t.datetime "created_at"
    t.datetime "updated_at"
end

The problem is that you cannot add yourself as friend, so I tried this in my model:

def validate
    if :user1_id == :user2_id
        record.errors.add "You cannot add yourself as a friend."
        return false
    end
end

And I have this in my controller:

def addfriend
    if params[:id]
        @friendship = Friendship.new()
        @friendship.user1_id = session[:user]
        @friendship.user2_id = params[:id]
        respond_to do |format|
            if @friendship.save
                format.html { redirect_to "/" } # Yes, SO users, I will fix this redirect later and it is not important for now.
                format.xml  { render :xml => @friendship, :status => :created }
            else
                format.html { redirect_to "/" }
                format.xml  { render :xml => @friendship.errors, :status => :unprocessable_entity }
            end
        end
    end
end

(where session[:user] is the uid of the currently signed in user)

However, when I go to http://localhost:3000/profile/addfriend/2.xml while I'm signed in as user 2, Rails returns me the new Friendship, instead of an error message, and when I take a look at my database, the Friendship is also there (and it shouldn't). Can someone please explain me how to fix this? Thanks

+1  A: 
if :user1_id == :user2_id

That's always gonna be false - you're comparing the symbols. It's the same as writing if "user1_id" == "user2_id".

You should write it as if user1_id == user2_id to compare the values of the columns.

sepp2k
+1 for explanation
Time Machine
+2  A: 

Try it like this:

class Friendship < ActiveRecord::Base
  validate :cannot_add_self

  def cannot_add_self
    errors.add_to_base('You cannot add yourself as a friend.') if self.user1_id == self.user2_id
  end
end
Jimmy Cuadra
√ for fixing entire problem
Time Machine