Rails, strict associations - ruby-on-rails

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

Related

Rails use model in the same namespace for belongs_to reference, how to reference model from outside

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

How can I validate an associated model?

Is my first time building a custom validation , since trying to the regular ORM validations did not work. I have a model called AdGroup which belongs to another model called Car. I want to send prevent that a user creates a new Ad Group if they have not selected a car. Also the Car is a file.
class AdGroup < ActiveRecord::Base
belongs_to :car
validate :validate_car_id
def validate_car_id
car = Car.find_by(id: params[:id])
if car.nil?
errors.add(:car, "Select a car image")
end
end
end
class Car < ActiveRecord::Base
validates :make, :model, :year, presence: true
validates :file, presence: true
belongs_to :make
has_many :ad_groups
...
end
Is an image that I am trying to select .
Your Ad Group model needs to be associated with the Car model. AdGroup models should have belongs_to :car line and the Car model needs has_many :ad_groups.
To validate associated models you could use ActiveRecord's validates_associated. Be sure to read the docs by the link for gotchas.
class AdGroup < ActiveRecord::Base
belongs_to :car
validates :car, presence: true
validates_associated :car
end
When using validates_associated you don't have to do custom validation.

How to access a variable from another controller in a form

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.

Rails belongs_to same model in multiple attributes

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

Rails has_many :through validation

I'm having trouble validating a model from a has_many through association. Below are the relevant models:
Broadcast Model
class Broadcast < ActiveRecord::Base
attr_accessible :content,
:expires,
:user_ids,
:user_id
has_many :users, through: :broadcast_receipts
has_many :broadcast_receipts, dependent: :destroy
validates :user_id, presence: true
validates :content, presence: true
end
Broadcast Receipt Model
class BroadcastReceipt < ActiveRecord::Base
belongs_to :broadcast
belongs_to :user
attr_accessible :user_id, :cleared, :broadcast_id
validates :user_id , presence: true
validates :broadcast_id , presence: true
end
There is also an association with Users that have_many broadcasts receipts through broadcast receipts.
The problem appears to be with the following line:
validates :broadcast_id , presence: true
Whenever I try to create a Broadcast, I get a rollback with no error messages given. However, when removing the above line, everything works as expected.
This looks like a problem with the Broadcast not being saved before the Broadcast Receipts are being created.
Is there any way I'd be able to validate the broadcast_id is set on the receipt model?
This appears to be the same issue discussed here: https://github.com/rails/rails/issues/8828, which was solved by adding :inverse of to the has_many associations to the join model.
There might be some problem in your code structuring. You could give this version a try.
class Broadcast < ActiveRecord::Base
# I assume these are the recipients
has_many :broadcast_receipts, dependent: :destroy
has_many :users, through: :broadcast_receipts
# I assume this is the creator
validates :user_id, :content, presence: true
attr_accessible :content, :expires, :user_id, :user_ids
end
class BroadcastReceipt < ActiveRecord::Base
belongs_to :broadcast
belongs_to :user
# You should be able to validate the presence
# of an associated model directly
validates :user, :broadcast, presence: true
attr_accessible :cleared
end

Resources