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
Related
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.
I working on a Rails application, currently we structure the app by modules. Right now we have 2 separate model for users: User and Freight::Customer::User.
I have a new model Freight::Customer::MembershipStatus looks like this:
class Freight::Customer::MembershipStatus < ActiveRecord::Base
belongs_to :customer, class_name: 'Freight::Customer'
belongs_to :created_by, class_name: 'User'
validates :from, presence: true
validates :to, presence: true
validates :customer, presence: true
validates :status, presence: true
end
In this case, the created_by is reference to User. But when the code run membership_status.created_by, rails try to look for the Freight::Customer::User, I think it because Rails try to look for model within the same module first.
Is there a way to config this model to use the outer User model class?
You can get user class using this type, try this.
class Freight::Customer::MembershipStatus < ActiveRecord::Base
belongs_to :customer, class_name: 'Freight::Customer'
belongs_to :created_by, class_name: '::User'
validates :from, presence: true
validates :to, presence: true
validates :customer, presence: true
validates :status, presence: true
end
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.
My full code can be seen at https://github.com/andyw8/simpleform_examples
I have a join model ProductCategory with the following validations:
validates :product, presence: true
validates :category, presence: true
My Product model has the following associations:
has_many :product_categories
has_many :categories, through: :product_categories
When I try to create a new product with a category, the call to #product.save! in the controller fails with:
Validation failed: Product categories is invalid
When I remove the validations, everything works and the join models are saved correctly.
I'm using strong_parameters but I don't think that should be related to this issue.
This is a "racing condition" in the callback chain.
When you create a product it doesn't have any id before it is saved, therefore there is no product in the scope of ProductCategory.
Product.new(name: "modern times", category_ids:[1, 2]) #=> #<Product id: nil >
At that stage of validation (before saving), ProductCatgory cannot assign any id to it's foreign key product_id.
That's the reason you have association validations : so that the validation happens in the scope of the whole transaction
UPDATE: As said in the comment you still can't ensure presence of a product/category. There's many ways around depending on why you want do this (e.g direct access to ProductCategory through some form)
You can create a flag to have validates :product, presence: true, if: :direct_access?
or if you can only update them: validates :product, presence: true, on: "update"
create your product first (in the products_controller) and add the categories after
... But indeed these are all compromises or workarounds from the simple #product.create(params)
Specifying inverse_of on your joining models has been documented to fix this issue:
https://github.com/rails/rails/issues/6161#issuecomment-6330795
https://github.com/rails/rails/pull/7661#issuecomment-8614206
Simplified Example:
class Product < ActiveRecord::Base
has_many :product_categories, :inverse_of => :product
has_many :categories, through: :product_categories
end
class Category < ActiveRecord::Base
has_many :product_categories, inverse_of: :category
has_many :products, through: :product_categories
end
class ProductCategory < ActiveRecord::Base
belongs_to :product
belongs_to :category
validates :product, presence: true
validates :category, presence: true
end
Product.new(:categories => [Category.new]).valid? # complains that the ProductCategory is invalid without inverse_of specified
Adapted from: https://github.com/rails/rails/issues/8269#issuecomment-12032536
Pretty sure you just need to define your relationships better. I still might have missed some, but hopefully you get the idea.
class Product < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates :name, presence: true
validates :description, presence: true
validates :color_scheme, presence: true
belongs_to :color_scheme
has_many :product_categories, inverse_of: :product
has_many :categories, through: :product_categories
end
class ProductCategory < ActiveRecord::Base
belongs_to :product
belongs_to :category
validates_associated :product
validates_associated :category
# TODO work out why this causes ProductsController#create to fail
# validates :product, presence: true
# validates :category, presence: true
end
class Category < ActiveRecord::Base
has_many :product_categories, inverse_of: :category
has_many :products, through: :product_categories
end