nested form associated model getting autosaved - ruby-on-rails

My associated model gets autosaved. I would like this to happen only when those fields contains any values. Obviously reject_if is the way to go for this but I cannot get it to work. I've tried :new_record? together with passing in the Proc. But I suspect something else is wrong here as the reject_posts method below does not get called.
has_one :reservation, as: :reservable, autosave: false,
class_name: Booking::Reservation
accepts_nested_attributes_for :reservation, reject_if: :reject_posts
def reject_posts(attributed)
binding.pry
end
How would I go about to try to debug this?

How would I go about to try to debug this?
The first step is to make sure your flow is invoking the accepts_nested_attributes_for method.
The simple check is to determine if your request console log sends the parameters as...
parameters: {
"model" => {
"reservation_attributes" => {...}
}
}
If your params don't feature _attributes, your fields_for or flow are incorrect.
--
You should also make sure your class_name option passes a string:
has_one :reservation, as: :reservable, class_name: "Booking::Reservation"
I would personally do the following:
#app/models/model.rb
class Model < ActiveRecord::Base
has_one :reservation, as: :reservable, class_name: "Booking::Reservation", inverse_of: :model
accepts_nested_attributes_for :reservation, reject_if: :all_blank?
end
The "rejection" is not validation. It just prevents the associated object from being passed to the nested model...
You may also set a :reject_if proc to silently ignore any new record hashes if they fail to pass your criteria
I'd use validations on the other model (with inverse_of):
#app/models/reservation.rb
class Reservation < ActiveRecord::Base
belongs_to :model, inverse_of: :reservation
validates :x, :y, :z, presence: true
end
--
If you wanted to reject based on specific attributes being blank, you should use:
#app/models/model.rb
class Model < ActiveRecord::Base
has_one :reservation, as: :reservable, class_name: "Booking::Reservation"
accepts_nested_attributes_for :reservation, reject_if: proc { |a| a['name'].blank? || a['title'].blank? }
end

Related

Accessing association in validates_with

I've created a custom validator in order to validate some attributes in my has_many association.
My class:
class User < ApplicationRecord
has_many :addresses
accepts_nested_attributes_for :addresses, allow_destroy: true
validates_with UniquenessMemoryValidator,
attributes: [:name],
collection: :addresses,
message: 'My custom message'
end
UniquenessMemoryValidator:
class UniquenessMemoryValidator < ActiveModel::Validator
def validate(record)
attrs, message = options.values_at(:attributes, :message)
collection = record[options[:collection]]
puts "collection #{collection}" # it's nil
end
end
The problem is that when I try to access my association (in this case: addresses) it prints nil.
So my question is: How can I access my "nested" array in validator?
PS: I can access any other attribute of my "record", less the association.
You can try with send if want to do it dynamically because it's usual method call:
collection = record.send(options[:collection])
Also, you can access to nested array only in accepts_nested_attributes_for validation with reject_if.
In reject_if you can pass block or method.
accepts_nested_attributes_for :addresses, reject_if: { |attrs| ... }
#OR
accepts_nested_attributes_for :addresses, reject_if: :my_method
def my_method
#some logic that returns true for invalid record
end

Validation Failed when using accepts_nested_attributes_for with Single Table Inheritance

First time I'm using STI, and I'm running into issues when trying to use accepts_nested_attributes_for with nested inherited objects.
class Document < ApplicationRecord
# code removed for brevity
end
class DocumentItem < ApplicationRecord
# code removed for brevity
end
class Package < Document
belongs_to :user
validates :title, :user, presence: true
has_many :package_items, dependent: :destroy
accepts_nested_attributes_for :package_items, reject_if: :all_blank, allow_destroy: true
end
class PackageItem < DocumentItem
belongs_to :package
end
When I try and use nested attributes, things stop working:
Package.create!(title: 'test',
user: User.last,
package_items_attributes: [{title: 'test'}])
Which results in the following error:
ActiveRecord::RecordInvalid: Validation failed: Package items package must exist
I've tried setting foreign_key and class_name on the belongs_to relationship, with no luck:
class PackageItem < DocumentItem
belongs_to :package, foreign_key: 'document_id', class_name: 'Document'
end
What am I doing wrong here?
UPDATE:
This seems to be an issue with Rails 5 and associations having required: true by default. When turning off required: true and setting foreign_key on the Invoice model, it correctly assigns the parent model ID and saves the parent model and child models.
Turns out it has nothing to do with STI, and is a known Rails 5 bug. :(
https://github.com/rails/rails/issues/25198

Why would one use reject_if if you can use validation?

Is there any reason you would use reject_if and do something like this?
class User < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, reject_if: proc do |attributes|
attributes['title'].blank?
end
end
Instead of using validation on the Post model?
class Post < ActiveRecord::Base
belongs_to :user
validates :title, presence: true
end
If you use validation, the creation of User will fail if there exists a Post which does not have a title.
If you use reject_if, the creation of User will succeed even if some, or all, of the Posts don't have titles. Only those posts without titles won't be saved.

ActiveRecord update validates model's association?

Given the relationship:
Library -> Books -> Category
Let's assume that category has some validations for instance:
validates :name, presence:true
If I run something like this:
Library.update(hash_with_all_attributes_from_library_to_category)
It will return true if all the library attributes are satisfied and ignore the category's validations.
why it happens? is there a better way to update all data except find one at time?
EDIT
I can't post the entire file but its a snippet
Class Question << ActiveRecord::Base
has_many :question_alternatives, dependent: :destroy, inverse_of: :question
# couple of validations
accepts_nested_attributes_for :question_alternatives, allow_destroy: true, reject_if: -> (attributes) {
attributes[:text].blank?
}
And I check for the other relationships that I have the accepts_nested_attributes_for and the inverse_of

How to validate presence of associated fields in Ruby on Rails

I have tables applications and business.
Applications
has_many :business
Business
belongs_to :applications
If I will create an Application, I should have at least one Business. I used link_to_add in the same form where I create applications.
I used validate_association :applciations but it didn't work.
You may be better to use validates_associated:
#app/models/business.rb
class Business < ActiveRecord::Base
belongs_to :application
validates :name, :other, param, presence: true
end
#app/models/application.rb
class Application < ActiveRecord::Base
has_many :businesses
validates_associated :businesses
end
This gives you the ability to check the validity of any associated data you pass through the model. However, this will not determine if there is at least one associated business passed through your model.
--
Numerical Validation
You'll want to look at the following
The reject_if method in Rails will not give you the ability to check the number of associated items which have been sent. This will have to be custom coded, which the author of the above post has addressed (in 2012) by setting a custom constant:
#app/models/company.rb
class Company < ActiveRecord::Base
OFFICES_COUNT_MIN = 1
validates :name, presence: true
validate do
check_offices_number
end
has_many :offices
accepts_nested_attributes_for :offices, allow_destroy: true
private
def offices_count_valid?
offices.count >= OFFICES_COUNT_MIN
end
def check_offices_number
unless offices_count_valid?
errors.add(:base, :offices_too_short, :count => OFFICES_COUNT_MIN)
end
end
end
I have not tested this myself, but to explain how it works, you'll basically have a custom validator, which allows you to check whether the number of associated objects is less than or equal to the CONSTANT you assign in the class.
You can, of course, achieve this without a constant, but the above example should demonstrate how you're able to create the functionality where at least 1 associated item should be sent
Not sure if this is what you meant but you could do the following in your models e.g.
has_many :businesses, dependent: :destroy
validates :title, presence: true
and in the other model:
belongs_to :application
validates :name, presence: true
You can use validates_presence_of
Validates that the specified attributes are not blank (as defined by Object#blank?), and, if the attribute is an association, that the associated object is not marked for destruction. Happens by default on save.
Example:
Applications
has_many :businesses
validates_presence_of :business
Business
belongs_to :applications
Update:
I think you will be better to using accepts_nested_attributes_for
Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off and you can enable it using the accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.
app/models/application.rb
Class Application < ActiveRecord::Base
has_many :businesses
accepts_nested_attributes_for :businesses, reject_if: :all_blank
end
#app/models/business.rb
Class Business < ActiveRecord::Base
belongs_to :application
end
This will give you the ability to call the reject_if: :all_blank method -
:reject_if
Allows you to specify a Proc or a Symbol pointing to a method that checks whether a record should be built for a certain attribute hash. The hash is passed to the supplied Proc or the method and it should return either true or false. When no :reject_if is specified, a record will be built for all attribute hashes that do not have a _destroy value that evaluates to true. Passing :all_blank instead of a Proc will create a proc that will reject a record where all the attributes are blank excluding any value for _destroy.

Resources