Reputation: 68
I'm pretty new to rails and find myself having some problems with understanding the relation between my database tables (i guess?).
My problem is the following:
I've got a Users table, containing information about users including their email addresses and another table containing "games" i'd like to manage those players played. When players want to submit their games, they have to specify the users participating in the game by their email addresses. I'd like to validate whether or not those players really exist.
My Game model:
class Game < ActiveRecord::Base
attr_accessible :caster1, :caster2, :faction1, :faction2, :player1, :player2, :points, :won
before_save { |game| player1 = player1.downcase }
before_save { |game| player2 = player2.downcase }
validate :existence_of_both_players
...
(some more validations)
...
private
def existence_of_both_players
User.exists?(:email => :player1.downcase) && User.exists?(:email => :player2.downcase)
end
end
My test case is the following:
require 'spec_helper'
describe Game do
before do
@game = Game.new( player1: "[email protected]",
faction1: "Some faction",
caster1: "Some caster",
player2: "[email protected]",
faction2: "Some other faction",
caster2: "Some other caster",
points: 35,
won: true)
end
...
(some other specs)
...
describe "saving a game" do
let(:player1) { User.create(name: "Example1",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar") }
let(:player2) { User.create(name: "Example2",
email: "[email protected]",
password: "foobar",
password_confirmation: "foobar") }
describe "with invalid players" do
describe "when both players do not exist" do
before { @game.player1 = @game.player2 = "[email protected]" }
it { should_not be_valid }
end
describe "when player1 does not exist" do
before { @game.player2 = player2 }
it { should_not be_valid }
end
describe "when player2 does not exist" do
before { @game.player1 = player1 }
it { should_not be_valid }
end
end
describe "with valid players" do
before do
@game.player1 = player1
@game.player2 = player2
end
it { should be_valid }
end
end
end
(Sorry for the mass of code, I just thought it would help).
My test are failing, and I'm pretty sure it's obvious why, sadly not for me.
Failures:
1) Game saving a game with invalid players when both players do not exist
Failure/Error: it { should_not be_valid }
expected valid? to return false, got true
# ./spec/models/game_spec.rb:108:in `block (5 levels) in <top (required)>'
...
I really can not figure out, why this is not working. I read the rails book and watched several screencasts, but none of them explains my problem properly.
Since this is my first post on stackoverflow, please let me know, if this post is too verbose. Thanks in advance.
Upvotes: 2
Views: 648
Reputation: 4808
A custom validator method should call errors.add to signal an error. So in your case something like:
def existence_of_both_players
player_one_exists = User.find_by_email :player1.downcase
player_two_exists = User.find_by_email :player2.downcase
unless player_one_exists and player_two_exists
errors.add :game, "both players need to exist"
end
end
Read more in the guide for ActiveRecord Validations and Callbacks.
Upvotes: 1