Rails ActiveRecord: validate single attribute - ruby-on-rails

Is there any way I can validate a single attribute in ActiveRecord?
Something like:
ac_object.valid?(attribute_name)

You can implement your own method in your model. Something like this
def valid_attribute?(attribute_name)
self.valid?
self.errors[attribute_name].blank?
end
Or add it to ActiveRecord::Base

Sometimes there are validations that are quite expensive (e.g. validations that need to perform database queries). In that case you need to avoid using valid? because it simply does a lot more than you need.
There is an alternative solution. You can use the validators_on method of ActiveModel::Validations.
validators_on(*attributes) public
List all validators that are being used to validate a specific
attribute.
according to which you can manually validate for the attributes you want
e.g. we only want to validate the title of Post:
class Post < ActiveRecord::Base
validates :body, caps_off: true
validates :body, no_swearing: true
validates :body, spell_check_ok: true
validates presence_of: :title
validates length_of: :title, minimum: 30
end
Where no_swearing and spell_check_ok are complex methods that are extremely expensive.
We can do the following:
def validate_title(a_title)
Post.validators_on(:title).each do |validator|
validator.validate_each(self, :title, a_title)
end
end
which will validate only the title attribute without invoking any other validations.
p = Post.new
p.validate_title("")
p.errors.messages
#=> {:title => ["title can not be empty"]
note
I am not completely confident that we are supposed to use validators_on safely so I would consider handling an exception in a sane way in validates_title.

I wound up building on #xlembouras's answer and added this method to my ApplicationRecord:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def valid_attributes?(*attributes)
attributes.each do |attribute|
self.class.validators_on(attribute).each do |validator|
validator.validate_each(self, attribute, send(attribute))
end
end
errors.none?
end
end
Then I can do stuff like this in a controller:
if #post.valid_attributes?(:title, :date)
render :post_preview
else
render :new
end

Building on #coreyward's answer, I also added a validate_attributes! method:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def valid_attributes?(*attributes)
attributes.each do |attribute|
self.class.validators_on(attribute).each do |validator|
validator.validate_each(self, attribute, send(attribute))
end
end
errors.none?
end
def validate_attributes!(*attributes)
valid_attributes?(*attributes) || raise(ActiveModel::ValidationError.new(self))
end
end

Related

Rails model common validations inherited from abstract base class but unique field validations occur on subclass

I'm trying to understand if it's possible, given two models that share some methods and fields, to put the validations that are common between the two of them in an abstract base class. Below code represents a simplified version of my situation.
There are two classes of invoice line items: sales and collections. These line items share a common field invoice_amount I want to validate the presence of the invoice_amount from an abstract base class but fields that are not common to both models get validated by the subclass.
class Collection < InvoiceLineItem
belongs_to :invoice
validates :c_number, :invoice_number, :invoice_date, presence: true
.
.
.
end
class Sale < InvoiceLineItem
belongs_to :invoice
.
.
.
end
class InvoiceLineItem < ApplicationRecord
self.abstract_class = true
def self.inherited(base)
super
base.send(:extend, NumberFormatter)
base.send(:commafy, :invoice_amount)
end
def invoice_amount
self[:invoice_amount] || '0.00'
end
def export_date
invoice_date
end
end
I've tried several things to get this to work with no success. Some of my attempts included adding the following code to my InvoiceLineItem base class
def self.inherited(base)
base.class_eval do
validates :invoice_amount, presence: true
end
super
base.send(:extend, NumberFormatter)
base.send(:commafy, :invoice_amount)
end
and
def self.inherited(base)
base.class_eval do
base.send(:validates, :invoice_amount, presence: true)
end
super
base.send(:extend, NumberFormatter)
base.send(:commafy, :invoice_amount)
end
and this as described here (https://medium.com/#jeremy_96642/deep-rails-how-to-use-abstract-classes-6aee9b686e75) which seemed promising because it described exactly what I want to do however it does not work for me.
with_options presence:true do
validates :invoice_amount
end
In all these cases the code executes without error however if I write a test like below it fails because validation succeeds!
RSpec.describe Collection, type: :model do
it "Requires an invoice amount" do
result = Collection.create(invoice_amount: nil, c_number: 'CUST012', invoice_number: 'INV001', invoice_date: Date.new(1999, 1,1))
expect(result.valid?).to be false
expect(result.errors[:invoice_amount]).to include("can't be blank")
end
end
I'm not really interested in hearing answers about how it should be done using composition instead of inheritance I won't go into the details but just assume that it has to be done using inheritance. It seems like it should be possible but I'm not sure and I can't find any source on the internet that has a solution that actually works.
Any help would be very much appreciated!
I figured out my issue, it turns out this code was working:
class InvoiceLineItem < ApplicationRecord
self.abstract_class = true
def self.inherited(base)
super
base.send(:extend, NumberFormatter)
base.send(:commafy, :invoice_amount)
end
with_options presence: true do
validates :invoice_amount
end
def invoice_amount
self[:invoice_amount] || 0.00
end
def export_date
invoice_date
end
def debtor_number_up_to_space
debtor_number.split[0]
end
end
the validator was using the method invoice_amount which was shadowing the field on the model so invoice_amount wasn't being seen as nil but as 0.00 thus my test validation was passing correctly.
Once I removed the || 0.00 I could get the test to fail.

How do a custom method applicable to various models

I have the following method called capitalizeEachWord. Inside this method there is an attribute called company
class BusCompany < ActiveRecord::Base
attr_accessible :company
before_save :capitalizeEachWord
validates :company,presence: true,
uniqueness: { case_sensitive: false },
format: /^([a-zA-z0-9]+\s?){1,}$/
def capitalizeEachWord
self.company=self.company.downcase.split.map(&:capitalize).join(' ')
end
end
I would like that this method not use the attribute company directly, but receives this attribute as a parameter for doesn't do it dependent of the model BusCompany. Something as the following. The problem is that this method I going to use in various models and don't want to write it in each model but use the inheritance
class BusCompany < ActiveRecord::Base
attr_accessible :company
before_save :capitalizeEachWord(self.company)
validates :company,presence: true,
uniqueness: { case_sensitive: false },
format: /^([a-zA-z0-9]+\s?){1,}$/
def capitalizeEachWord(attribute)
self.attribute=self.attribute.downcase.split.map(&:capitalize).join(' ')
end
end
Add the following code into config/initializers/capitalizer.rb
module Capitalizer
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def capitalize(*attributes)
#attributes_to_capitalize = attributes
before_save :capitalize_each_word
end
def attributes_to_capitalize
Array.new(#attributes_to_capitalize)
end
end
def capitalize_each_word
self.class.attributes_to_capitalize.each do |attr|
if value = send(attr)
self.send("#{attr}=", value.strip.titleize)
end
end
end
end
And then in your class:
class BusCompany < ActiveRecord::Base
include Capitalizer
capitalize :company
...
end
First, I'd recommend you override the setter for company instead of using error prone callbacks, like this:
class BusCompany < ActiveRecord::Base
# you can also use #titleize instead of capitalize each word
# also use try, in case `arg` is nil
def company=(arg)
super arg.try(:titleize)
end
end
Then you can use modules to wrap this functionality into a reusable unit. Throw this in a file in your concerns folder, or just in to the models folder:
module CapitalizedSetter
def capitalize_setter(*attr_names)
# for each attr name, redifine the setter so it supers the titleized argument instead
attr_names.each do |attr|
define_method(:"#{attr}=") { |arg| super arg.try(:titleize) }
end
end
end
Finally extend it into the desired models:
class BusCompany
extend CapitalizedSetter
capitalized_setter :company
end

Rails validation - confirming presence of form attribute that is not saved in model

My application has a form_for tag with element :foo that is not saved in the model for the object used in form_for.
I need to confirm that the user submitted a value for this element, using Rails Validation Helpers. However, the 'presence' validator makes a call to object.foo to confirm that it has a value. Since foo is not saved as part of my object, how can I do this validation?
Thanks!
You should probably check for the presence of it in the params in your controller action:
def create
#model = MyModel.find(params[:id])
unless params[:foo].present?
#model.errors.add(:foo, "You need more foo")
end
# ...
end
If :foo is an attribute of your object that isn't saved in the database and you really want to use ActiveRecord Validations, you can create an attr_accessor for it, and validate presence like this.
class MyModel < ActiveRecord::Base
attr_accessor :foo
validates :foo, presence: true
end
But that could result in invalid records being saved, so you probably don't want to do it this way.
Try this..
class SearchController < ApplicationController
include ActiveModel::ForbiddenAttributesProtection
def create
# Doesn't have to be an ActiveRecord model
#results = Search.create(search_params)
respond_with #results
end
private
def search_params
# This will ensure that you have :start_time and :end_time, but will allow :foo and :bar
params.require(:val1, :foo).permit(:foo, :bar , whatever else)
end
end
class Search < ActiveRecord::Base
validates :presence_of_foo
private
def presence_of_foo
errors.add(:foo, "should be foo") if (foo.empty?)
end
end
See more here

Rails order Post by def in model

I want to order posts based on the total votes it has. This is what I have in the Post Model:
class Post < ActiveRecord::Base
attr_accessible :title, :url
validates :title, presence: true
validates :url, presence: true
has_many :votes
def vote_number
votes.where(direction: "up").count - votes.where(direction: "down").count
end
end
And this is what I attempted to do in the Post Controller:
def index
#posts = Post.last(10).order('vote_number')
end
Nevertheless I get this error from the index:
undefined method `order' for #<Array:0x3787158>
The other questions in Stack Overflow resolved this problem by making the calculation in the Post Controller but I can not do it because votes are arrays and not integers.
Found a way to solve it. Instead of using order I used sort_by.
Instead of having this in the Post Controller:
def index
#posts = Post.last(10).order('vote_number')
end
I used sort_by:
def index
#posts = Post.all.sort_by{|post|-post.vote_number}
end
You should try counter cache.
You can read more about it from the following links -
How to sort authors by their book count with ActiveRecord?
http://hiteshrawal.blogspot.com/2011/12/rails-counter-cache.html
http://railscasts.com/episodes/23-counter-cache-column
Counter cache only works inside rails. If you updated from outside application you might have to do some work around.
first, last and all execute query. Insert order always before those three.
class Post < ActiveRecord::Base
attr_accessible :title, :url
attr_reader :vote_difference # getter
attr_writer :vote_difference # setter
validates :title, presence: true
validates :url, presence: true
has_many :votes
end
class Vote < ActiveRecord::Base
belongs_to :post, :counter_cache => true
#more class methods here
def after_save
self.update_counter_cache
end
def after_destroy
self.update_counter_cache
end
def update_counter_cache
post.vote_difference = post.comments.where(direction: 'up').count - post.comments.where(direction: 'down').count
post.save
end
end
Now you can sort by vote_difference when you query up.
for example -
posts = Post.order(:vote_difference, :desc)
I haven't check the correctness of my code yes. If you find any issues please let me know. I am sure it can be adapted to make it works.
If you follow this pattern to use counter_cache you might will to run a migration to add a vote_difference column, and another migration to update the vote_difference column for previous created post.

Custom validation in Rails

I have a Post has_many Comments association. Post has boolean attribute published.
When post.published is false, the new comment shouldn't be valid.
What is the best practise to accomplish this kind of validation?
I've tried to do it by this way, but sadly, it doesn't work correctly. It is still possible to create new comment for unpublished post.
class Comment < ActiveRecord::Base
validates :post_id, presence: true, if: :post_is_published
...
def post_is_publised
post && post.published
end
end
Hmm.. I think you have syntax errors in your code... Try this:
class Comment < ActiveRecord::Base
validates :post_id, :presence => true, :if => :post_is_published
def post_is_publised
post.try(:published)
end
end
After reading your console output and checking your question one more time:
class Comment < ActiveRecord::Base
validate :post_has_to_be_published
def post_has_to_be_published
unless post.try(:published)
self.errors.add(:base, "you can add comments only to published posts")
end
end
end
I understand that you don't want to allow adding comments to unpublished posts. Above code should accomplish that.

Resources