Why doesn't custom validation work in RoR 3 - ruby-on-rails

Here is my code for the Photo model class:
class Photo < ActiveRecord::Base
belongs_to :user
belongs_to :organization
validate :user_id_or_organization_id_cant_be_blank
...
def user_id_or_organization_id_cant_be_blank
if !:user_id? and !:organization_id?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
end
The problem is in the validation. It doesn't work, and i don't understand why.
I can create a photo with no user_id or organization_id which is not supposed to happen.
Please explain to me what i'm doing wrong?

You'd rather do:
def user_id_or_organization_id_cant_be_blank
if user_id.blank? && organization_id.blank?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
end

Rails provides the standard approach to check the presence:
validates :user_id, :organization_id, presence: true
If you need to do a complex validation, try to use ActiveModel::Validator
class Photo < ActiveRecord::Base
validates_with UserOrganizationIdsValidator
end
class UserOrganizationIdsValidator < ActiveModel::Validator
def validate(record)
if user_id.blank? && organisation_id.blank?
errors.add(:user_id, "no user")
errors.add(:organisation_id, "no organization")
end
# something custom else...
end
end

Related

confusion over validates_with custom validations syntax shown in rails guide

I am attempting to use the validates_with custom validations helper with Rails 4.
The following code is working in my application:
class Photo
validates_with CleanValidator
include ActiveModel::Validations
end
class CleanValidator < ActiveModel::Validator
def validate(record)
if record.title.include? "foo"
record.errors[:title] << "Photo failed! restricted word"
end
end
end
However I want to pass this helper to multiple attributes in multiple models, not just :title.
There is an example in validates_with section of guide that contains the following example:
class GoodnessValidator < ActiveModel::Validator
def validate(record)
if options[:fields].any?{|field| record.send(field) == "Evil" }
record.errors[:base] << "This person is evil"
end
end
end
class Person < ApplicationRecord
validates_with GoodnessValidator, fields: [:first_name, :last_name]
end
This is what I want to achieve, substituting [:fields] for [:title] in my code example so that I can use CleanValidator for multiple models and multiple attributes (User.name, Photo.title etc).
I think you want the other example from the guides, each validator. You should be able to do
class CleanValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unless ["Evil", "Other", "Restricted", "Words"].include?(value)
record.errors[attribute] << (options[:message] || "is a restricted word")
end
end
end
class Photo
include ActiveModel::Validations
attr_accessor :title
validates :title, clean: true
end

NoMethodError at / undefined method `name' for nil:NilClass

I have tried many solution and came up with good one but still getting error. I am editing my whole question.
I am trying to create Friendly URL with friendly_id gem.
In my project First user need to signup with devise.
Devise will pass some information to profile model with
model/user.rb
delegate :age, :city, :region, :country, to: :profile
I want to make user.name to be Friendly_id candidate. I have tried following code in my Profile model:-
class Profile < ActiveRecord::Base
extend FriendlyId
friendly_id :user_name , use: :slugged
def user_name
user.name
end
But it is giving error
NoMethodError at /
undefined method `name' for nil:NilClass
now After submitting user form.
Please suggest possible solution with explanation.
My User.rb looks like
require 'open-uri'
class User < ActiveRecord::Base
paginates_per 10
validates :name , presence: true, length: { maximum: 200 }
scope :by_name, ->(name) do
joins(:profile).where('lower(name) LIKE ?', "%#{name.downcase}%")
end
delegate :age, :city, :region, :country, to: :profile
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile
def self.new_with_session(params, session)
session_params = { 'profile_attributes' => {} }
provider = session['devise.provider']
if provider && data = session["devise.#{provider}"]
session_params['name'] = data[:name] if data[:name]
session_params['email'] = data[:email] if data[:email]
session_params['profile_attributes'] =
{ avatar: data[:image] } if data[:image]
end
params.deep_merge!(session_params)
super.tap do |user|
if auth = Authorization.find_from_session(session, provider)
user.authorizations << auth
end
end
end
def password_required?
super && registered_manually?
end
def registered_manually?
encrypted_password.present?
end
end
And my profile.rb looks like
class Profile < ActiveRecord::Base
extend FriendlyId
friendly_id user.name, use: :slugged
belongs_to :user
accepts_nested_attributes_for :user
validates :website, allow_blank: true, uri: true
def website=(url_str)
if url_str.present?
url_str = "http://#{url_str}" unless url_str[/^https?/]
write_attribute :website, url_str
end
end
end
I think Problem is here:
Request parameters
{"action"=>"new", "controller"=>"users/registrations"}
Please suggest possible solution and explanation.
And users/registration:
class Users::RegistrationsController < Devise::RegistrationsController
layout 'land'
def create
params[:user][:profile_attributes].delete(:place)
end
protected
def after_sign_up_path_for(resource)
welcome_path
end
end
I am creating user in profile controller
def load_profile
#profile = Profile.friendly.find(params[:id])
if !#profile || #profile.user.blocked_users.include?(current_user)
redirect_to home_path
else
#user = #profile.user
end
end
#Rodrigo helped me find out error that error is due to Friendly_id can't create link with user instance.
There is an error on this line:
friendly_id user.name, use: :slugged
The variable user doesn't exists at Profile class scope. You should use something like this:
friendly_id :user_name, use: :slugged
def user_name
user.name
end
extend FriendlyId
friendly_id u_name, use: :slugged
def u_name
user.name
end
belongs_to :user
Have you defined user? what is user.name?

Generic method for validation of paperclip files in different models

I have 2 models users , companies
User model:
has_attached_file :avatar,
...
:whiny=>false
validates_with ImageSizeValidator
validates_with ImageTypeValidator
validates_with ImageConvertionValidator
Company model:
has_attached_file :logo,
#the rest is similar
I have done validation for users and put it in validation_helper
class ImageSizeValidator < ActiveModel::Validator
def validate(record)
if record.avatar_file_name.present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.avatar_file_size") unless record.avatar_file_size.to_i < 200000
end
end
end
class ImageTypeValidator < ActiveModel::Validator
def validate(record)
if record.avatar_file_name.present?
record.errors[:base] << (I18n.t :file_type, scope: "activerecord.errors.models.user.attributes") unless ['image/jpeg', 'image/gif','image/png'].include?(record.avatar_content_type)
end
end
end
My problem is that the names will be different so avatar_file_name for users and logo for companies.
Do I have to do a specific method for each?
How can I work this around?
you just need to add options. If you take a look at documentation, you can pass arguments in block:
#model
validates_with ImageSizeValidator, paperclip_field_name: :avatar
#validator
def validate(record)
if record.send(options[:paperclip_field_name].to_s+"_file_name").present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.#{options[:paperclip_field_name]}_file_size") unless record.send(options[:paperclip_field_name].to_s+"_file_name").to_i < 200000
end
end
but much easier to use validate_each method
#model
validates :avatar, image_size: true, image_type: true, image_conversion: true
#validator
def validate_each(record, attribute, value)
if record.send(attribute.to_s+"_file_name").present?
record.errors[:base] << (I18n.t :in_between, scope: "activerecord.errors.models.user.attributes.#{attribute}_file_name)") unless record.send(attribute.to_s+"_file_name").to_i < 200000
end
end
Paperclip has built-in support for validations (https://github.com/thoughtbot/paperclip#validations). If their validation are not a fit for your problem you can look on how they are doing it: https://github.com/thoughtbot/paperclip/tree/master/lib/paperclip/validators

Using Roles for Validations in Rails

Is it possible to use the roles used for attr_accessible and attr_protected? I'm trying to setup a validation that only executes when not an admin (like this sort of http://launchware.com/articles/whats-new-in-edge-scoped-mass-assignment-in-rails-3-1). For example:
class User < ActiveRecord::Base
def validate(record)
unless # role.admin?
record.errors[:name] << 'Wrong length' if ...
end
end
end
user = User.create({ ... }, role: "admin")
After looking into this and digging through the source code, it appears that the role passed in when creating an Active Record object is exposed through a protected method mass_assignment_role. Thus, the code in question can be re-written as:
class User < ActiveRecord::Base
def validate(record)
unless mass_assignment_role.eql? :admin
record.errors[:name] << 'Wrong length' if ...
end
end
end
user = User.create({ ... }, role: "admin")
Sure can would be something like this:
class User < ActiveRecord::Base
attr_accessible :role
validates :record_validation
def record_validation
unless self.role == "admin"
errors.add(:name, "error message") if ..
end
end
You could do this
class User < ActiveRecord::Base
with_options :if => :is_admin? do |admin|
admin.validates :password, :length => { :minimum => 10 } #sample validations
admin.validates :email, :presence => true #sample validations
end
end
5.4 Grouping conditional validations

Rails 3: Uniqueness validation for nested fields_for - Part2

I am new to coding - and have not enough reputation to comment this answer:
Rails 3: Uniqueness validation for nested fields_for
So I am creating this question as "Part 2" :)
I am a web designer but curious to learn coding, held with this from my days.
# app/validators/nested_attributes_uniqueness_validator.rb
class NestedAttributesUniquenessValidator < ActiveModel::EachValidator
record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
end
end
above code with "ActiveModel::EachValidator" throw this error:
"undefined method `map' for "Area 1":String"
# app/validators/nested_attributes_uniqueness_validator.rb
class NestedAttributesUniquenessValidator < ActiveModel::Validator
record.errors[attribute] << "Products names must be unique" unless value.map(&:name).uniq.size == value.size
end
end
above code with "ActiveModel::Validator" throw this error:
"Subclasses must implement a validate(record) method. "
this is model file:
class Area < ActiveRecord::Base
validates :name,
:presence => true,
:uniqueness => {:scope => :city_id},
:nested_attributes_uniqueness => {:field => :name}
belongs_to :city
end
You can find complete code over here:
https://github.com/syed-haroon/rose
#Syed: I think you are trying to do this. else reply to my comment.
# app/models/city.rb
class City < ActiveRecord::Base
has_many :areas
validates :areas, :area_name_uniqueness => true
end
# app/models/area.rb
class Area < ActiveRecord::Base
validates_presence_of :name
validates_uniqueness_of :name
end
# config/initializers/area_name_uniqueness_validator.rb
class AreaNameUniquenessValidator < ActiveModel::Validator
def validate_each(record, attribute, value)
record.errors[attribute] << "Area names must be unique" unless value.map(&:name).uniq.size == value.size
end
end
I found the answer over here :
https://rails.lighthouseapp.com/projects/8994/tickets/2160-nested_attributes-validates_uniqueness_of-fails
&
validates_uniqueness_of in destroyed nested model rails
This is for rails 2, one line need to me modified over here:
add_to_base has been deprecated and is unavailable in 3.1. Use self.errors.add(:base, message)

Resources