I am trying to write a unit testing for a User model in Ruby on Rails. I am using authlogic and need to check that the first_name and last_name of the user model attributes are not the same when the user is registering.
This is my user model:
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field= :username
end
has_many :memberships, :class_name => "Project::Membership"
has_many :projects, :through => :memberships
has_one :profile
validates :email, :presence => true, :uniqueness => true
validates :username, :presence => true, :uniqueness => true
validates :first_name,:presence => true
validates:last_name, :presence => true
validates :title, :presence => true
validates :password, :presence => true
validates :password_confirmation, :presence => true
validates :gender, :presence => true
# Custom validator
validates :first_name, :last_name, :different_names => true
As you can see, I tried to create a custom validator creating a new file in /lib/different_names_validator.rb with a class called DifferntNamesValidator, but couldn't get it, as I got the following error: Unknown validator: 'different_names' (ArgumentError)
Thanks in advance!
Hi Try to include this module in your model
Related
When updating car info, the validation process fails because I have validates_uniqueness_of :number
class Car < ActiveRecord::Base
validates :number,numericality: true, length: {is: 7 }
validates :number, :name, presence:true
validates_uniqueness_of :number, :message => "מספר רכב זה קיים במערבת"
belongs_to :owner
has_many :visits
end
I need validation to pass, if the original value was not changed, validation on_create would not help since I still need validation when updating.
Any help would be really appreciated.
This will work for you :
validates :number, :uniqueness => {:scope => :number}, :message => "מספר רכב זה קיים במערבת"
OR
validates_uniqueness_of :number, :message => "מספר רכב זה קיים במערבת", :scope => :number
I have a nested recourse called "transactions" inside another recourse "budgets".
All I'm trying to accomplish is for my users to be able to edit individual "transactions". However when I go to /1/transactions/1/edit I get a LocalJumpError saying "no block given (yield)".
There might be a very simple solution to this but I haven't been able to find it yet.
routes.rb:
resources :budgets, :path => '/' do
resources :transactions
end
budget.rb:
class Budget < ActiveRecord::Base
belongs_to :user
has_many :transactions
validates :amount, presence: true
validates :title, presence: true
validates :user, presence: true
validates :amount, numericality: true
extend FriendlyId
friendly_id :title, use: :slugged
def should_generate_new_friendly_id?
new_record?
end
end
transaction.rb
class Transaction < ActiveRecord::Base
belongs_to :user
belongs_to :budget
validates :amount, presence: true
validates :user, presence: true
validates :budget, presence: true
validates :date, presence: true
validates :amount, numericality: true
validates :is_positive, :inclusion => {:in => [true, false]}
end
transactions_controller.rb
def edit
#budget = Budget.friendly.find(params[:budget_id])
#transaction = #budget.transaction
end
And in the view transactions/edit.html.erb:
<%= form_for(#transaction) do |f| %>
What am I missing?
Naming a model Transaction conflicts with ActiveRecord::Transactions. You'll need to rename your model.
http://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html
I want to do STI in Rails.
class AbstractUser < ActiveRecord::Base
self.table_name = 'users'
belongs_to :organization, :inverse_of => :users
# reporter user
has_many :requests, :dependent => :destroy
# startup user
has_many :responses, :dependent => :destroy
has_many :startup_requests, :through => :responses, :source => :request
scope :reporters, where(:type => 'Reporter')
scope :startup_employees, where(:type => 'Startup')
scope :on_waitlist, where(:waitlist => true)
scope :not_on_waitlist, where(:waitlist => false)
end
require 'rfc822'
class User < AbstractUser
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :confirmable
validates :name, :presence => true
validates :surname, :presence => true
validates :title, :presence => true
validates :password, :presence => true, :length => { :minimum => 8 }
validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE }
attr_accessible :name, :surname, :title, :organization,
:email, :password, :fullname
end
require 'rfc822'
class UserForAdmin < AbstractUser
validates :email, :presence => true, :format => { :with => RFC822::EMAIL_REGEXP_WHOLE }
validates :organization_id, :presence => true
attr_accessible :name, :surname, :title, :organization, :email,
:password, :fullname, :password_confirmation, :type,
:organization_id, :waitlist, :invitation_token
end
And there is some problem with these scopes.
Couldn't find UserForAdmin with id=7 [WHERE "users"."type" IN ('UserForAdmin') AND "users"."waitlist" = 'f']
I also tried to put these scopes in UserForAdmin instead of AbstractUser with the same result. I (probably) need scopes instead of custom methods, because I use them in ActiveAdmin. How can I solve this?
If you don't want to receive all users, you need to query with the base class. In a simpler example:
class Animal < ActiveRecord::Base
end
class Dog < Animal
end
class Cat < Animal
end
Dog.create
Cat.create
Animal.all
=> [dog, cat]
Dog.all
=> [dog]
Cat.all
=> [cat]
So, in your case, you'd want to:
AbstractUser.not_on_waitlist.find(params[:id])
If this user is a UserForAdmin you'll receive an object of class UserForAdmin. If it's just a user, you'll receive an object of class User
I have the following model in rails (simplified):
class Phone < ActiveRecord::Base
include ActiveModel::Validations
belongs_to :user
belongs_to :brand
attr_readonly :user, :brand
attr_accessible :model, :phone_number
validates :user, :presence => true
validates :brand, :presence => true
validates :model, :presence => true
validates :phone_number, :presence => true
end
According to the documentation, attr_readonly should allow attributes to be set at creation, but not at update.
However, when I do this:
Phone.create(:user => <existing_user>, :brand => <existing_brand>, :model => "Galaxy", :phone_number => "555-433-5678")
I get this error:
Can't mass-assign protected attributes user, brand
What am I missing?
If you want to assign a user and a brand association like that, you must define them as being accessible attributes:
attr_accessible :user, :brand
Otherwise, you can assign them like this:
Model.create({:user => user, :brand => brand }, :without_protection => true)
Suppose I have:
class Author
has_many :books
class Book
belongs_to :author
validates :name, :presence => true, :uniqueness => true
I want to change this so that the name of the book is only unique within the scope of the author, i.e. no author has two books with the same name, but two authors could have a book with the same name. Is this possible?
It's very possible and quite easy:
validates :name, :presence => true, :uniqueness => {scope: :author}
validates :name, :presence => true, :uniqueness => {:scope => :author_id}