KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
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
Tags (comma-separated)
Save Edits
Cancel