I have a form to collect information about a Physician. Each Physician can have at least one Address but it's ok for a physician not to have any address.
Below is the relevant code from my physician.rb and model.rb files that define the respective models.
class Physician < ApplicationRecord
belongs_to :address, optional: true
accepts_nested_attributes_for :address
class Address < ApplicationRecord
validates :line_1, presence: true
validates :city, presence: true
Address has some required fields, line_1, and city
I would like to ignore the presence requirement on these fields ONLY for the Physician form.
I tried using the optional: true tag (shown in the code above) but that didn't do it.
You probably need to change the following line
accepts_nested_attributes_for :address
to
accepts_nested_attributes_for :address, reject_if: :all_blank
so the associated Address object will be completely ignored if all of its attributes are left blank.
Related
I have created my model:
class Vehicle < ActiveRecord::Base
belongs_to :player
has_one :basis
has_one :carcass
has_one :weapon
end
How I can make this model able to save itself ONLY when it got its player, basis, carcass, weapon? What I need to ad to my validation?
You can validate the state of objects before they go into the database using Active Record's validations feature.
In your Vehicle model, define your validations as follows:
class Vehicle < ActiveRecord::Base
belongs_to :player
has_one :carcass
has_one :weapon
validates :player, presence: true
validates :carcass, presence: true
validates :weapon, presence: true
end
This should prevent your Vehicle to be saved if any of your references is missing.
Good luck!
Edit
You can shorten the validation rules:
validates :player, :carcass, :weapon, presence: true
My question is kind of a generalized one and I don't really know how to provide code for it. But, I was wondering if I could pass variables into a new page containing a form. Basically, the gist of my problem is I have a carpools table, a trips table, and a users table. The relevant models are shown below
users
class User < ActiveRecord::Base
#associations
has_many :users_trips
has_many :carpools
has_many :trips, :through => :users_trips
end
trips
class Trip < ActiveRecord::Base
#including the wysiwyg editor
include Bootsy::Container
#associations
belongs_to :user
has_many :carpools
has_many :users_trips
has_many :users, :through => :users_trips
#make sure trips get ordered from newest to oldest
default_scope -> { order(start_date: :desc) }
#validations
validates :name, presence: true, length: { maximum: 50 }
validates :description, presence: true
validates :start_date, presence: true
validates :end_date, presence: true
end
and carpools
class Carpool < ActiveRecord::Base
#associations
belongs_to :trip
belongs_to :user
#validations
validates :trip_id, presence: true
validates :make, presence: true
validates :user_id, presence: true
validates :model, presence: true
validates :leave_time, presence: true
validates :seats, presence: true
validates :year, presence: true
end
Also, if it helps, i have a many-to-many relationship table called users_trips. I am currently trying to have a user have the ability to sign his/her car up for a trip (hence a carpools table). But when I began to think about the form for the carpool, I realized I was going to have a problem passing the trip_id to the carpool. Assume each of the respective controllers for users/trips are correct. The controller for the carpools has not been made yet.
Also, if it helps, I currently have the idea of, on the trip page, there will be a button that allows the user to sign his/her car up for the trip. That button will lead to the form where the user can fill in the respective fields for their car.
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.
There is a model called Student and has an attribute called :studies_level.
studies_level can have one of the following values: ['school_graduate', 'undergraduate', 'graduate', 'postgraduate', 'doctoral', 'postdoctoral']
There is also another model called University
If :studies_level is postgraduate then Student must fill the following attributes :undergraduate_university and :postgraduate_university with a University_id
Is there any way to achieve this with Rails model relations (belongs_to, etc) ?
belongs_to :undergraduate_university, class_name: :University
belongs_to :postgraduate_university, class_name: :University
validates :undergraduate_university, presence: true, if: :studies_level=='postgrad'
validates :postgraduate_university, presence: true, if: :studies_level=='postgrad'
validates_associated :undergraduate_university
validates_associated :postgraduate_university
I have 2 models: Provider and Delivery. The Note model belongs to either of the two, by polymorphism (belongs_to).
The Provider model looks like this:
class Provider < ActiveRecord::Base
attr_accessible :name, :site_url, :brand_ids, :note_attributes
validates :name, presence: true
has_one :note
accepts_nested_attributes_for :note, allow_destroy: true
end
The form to create a new provider renders with no problem, but when I try to save it I get the following error:
unknown attribute: provider_id
How can I fix this?
The Note model should have attr_accessible :provider_id