I have a User
model which has many roles
. Roles contains a user_id
field, which I want to validate_presence_of
The is
I think you can get around the validation problem if you change your code to look like this:
@user = User.new(params[:user])
@user.roles.new(:name => 'Peon') unless @user.has_roles?
if @user.save
# ...
If that doesn't work, you could try changing you validation to this:
class Role < ActiveRecord::Base
belongs_to :user
validates :user_id, :presence => true, :unless => Proc.new() {|r| r.user}
end