开发者

Two columns must not equal each other in Rails

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
   开发者_如何学Go             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


Try it like this:

class Friendship < ActiveRecord::Base
  validate :cannot_add_self

  private

  def cannot_add_self
    errors.add(:user2_id, 'You cannot add yourself as a friend.') if user1_id == user2_id
  end
end


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.


more fun:

  validates :user1_id, exclusion: { in: ->(friendship) { [friendship.user2_id] }, message: "Stop it or you'll go blind." }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜