Here are two models
Product and Variant
class Product.rb
has_many :variants, dependent: :destroy
end
class Variant.rb
belongs_to :product
end
Variant.create(product_id:rand(500..3000))
The above line creates a variant in the db even if product is not present with that particular id.
To handle this,
in before_create I could query and check if product is present.
Other than this is there any in-built method or function that rails provides to handle such cases.
It should be like below in variant.rb
class Variant < ApplicationRecord
belongs_to :product
end
In Rails 5, whenever we define a belongs_to association, it is required to have the associated record present by default after this change.
In Rails 4.x world To add validation on belongs_to association, we need to add option required: true .
class Variant < ApplicationRecord
belongs_to :product, required: true
end
Try:
class Variant.rb
belongs_to :product
validates :product, presence: true
end
According to the docs, this will ensure the product record actually exists.
BTW, it should be belongs_to :product, not belongs_to :products.
In Rails 5 by default the reference is required when you specify a belongs_to relation. You need to specify optional: true in case you do not want any compulsion on reference presence.
In Rails 4, you need to specify required: true to ensure the reference object is present and valid.
belongs_to :product, required: true
You can checkout the blogpost below:
https://blog.bigbinary.com/2016/02/15/rails-5-makes-belong-to-association-required-by-default.html
Hope this helped.
Related
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
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.
I was reading validation from http://guides.rubyonrails.org/active_record_validations.html . I understand that presence checks whether given attribute is either empty or consists of whitespace. But what I don't understand is that, how to test association is present. They showed two example
class LineItem < ActiveRecord::Base
belongs_to :order
validates :order, presence: true
end
and
class Order < ActiveRecord::Base
has_many :line_items, inverse_of: :order
end
I understand the code here but I don't understand how it test association.
LineItem has order_id and that is what being validated. it is simply checking if that field/column has a value.
I'm trying to validate a model Student like this;
class Student < ActiveRecord::Base
belongs_to :room
end
I want to ensure that Room is a valid model, and with that I can only save a student only if the room is valid.
I tried to change the association to:
belongs_to :room, :validate => true
But it didnt change the behaviour..
API says:
:validate
If false, don’t validate the associated objects when saving the parent object. false by default.
So I changed the validation to room:
class Room < ActiveRecord::Base
has_many :students, :validate => true
end
but neither options solve for me
any ideas???
Give this a try...
class Student < ActiveRecord::Base
belongs_to :room
validates_associated :room
end
I'm looking at this portion of the API docs: http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#method-i-validates_associated
Also, be careful not to use that validation on both sides of the association!
I am using Ruby on Rails 3 and I would like to change the default type-column name used by a polymorphic association.
For example, if I have this class:
class Comment < ActiveRecord::Base
...
end
and I implement for that a polymorphic association, I would like to use the type-column names comm_id and comm_type instead of commentable_id and commentable_type. Is it possible? If so, what I have to change for the Comment class?
There's no way in Rails API to override the default column name used for polymorphic associations.
Take a look at this answer for a possible solution.
I did this in rails 6 on my legacy database using the foreign_type: option. This should work for rails >= 4.2.1 (see here)
# booking model
class Booking < ApplicationRecord
has_many :user_notes, as: :notable, foreign_type: :note_type, foreign_key: :type_id
end
# booking model
# here polymorphic columns are `note_type` and `type_id`
class UserNote < ApplicationRecord
belongs_to :notable, polymorphic: true, foreign_type: :note_type, foreign_key: :type_id
end
In your case, why not just change the association to:
# Comment
belongs_to :comm, :polymorphic => true
# Everything else
has_many :comments, :as => :comm