Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify an optional reference in your Rails model

I have a Sponsors model and a Promo Codes model.

  • A sponsor can have zero or more promo codes
  • A promo code can have zero or one sponsors

Thus a promo code should have an optional reference to a sponsor, that is, a sponsor_id that may or may not have a value. I'm not sure how to set this up in Rails.

Here's what I have so far:

# app/models/sponsor.rb
class Sponsor < ActiveRecord::Base
  has_many :promo_codes  # Zero or more.
end

# app/models/promo_code.rb
class PromoCode < ActiveRecord::Base
  has_one :sponsor  # Zero or one.
end

# db/migrate/xxxxx_add_sponsor_reference_to_promo_codes.rb
# rails g migration AddSponsorReferenceToPromoCodes sponsor:references
# Running migration adds a sponsor_id field to promo_codes table.
class AddSponsorReferenceToPromoCodes < ActiveRecord::Migration
  def change
    add_reference :promo_codes, :sponsor, index: true
  end
end

Does this make sense? I'm under the impression that I have to use belongs_to in my Promo Codes model, but I have no basis for this, just that I've haven't seen a has_many with has_one example yet.

like image 748
Dennis Avatar asked Feb 24 '14 16:02

Dennis


People also ask

What is optional true in rails?

If you set the :optional option to true, then the presence of the associated object won't be validated. By default, this option is set to false . otherwise it will be required associated object.

What is polymorphic association in Rails?

In Ruby on Rails, a polymorphic association is an Active Record association that can connect a model to multiple other models. For example, we can use a single association to connect the Review model with the Event and Restaurant models, allowing us to connect a review with either an event or a restaurant.

How would you choose between Belongs_to and Has_one?

The difference between belongs_to and has_one is a semantic one. The model that declares belongs_to includes a column containing the foreign key of the other. The model that declares has_one has its foreign key referenced.

Can foreign key be null rails?

add_foreign_key simply adds a foreign key constraint whether the field is required or not (in your case author_id in articles ). Did you get an error when you tried this in your migration? SO, if in your original migration of articles , author_id is null, then you can have foreign key that's nullable.


3 Answers

This looks like a simple has_many and belongs_to relationship:

# app/models/sponsor.rb
class Sponsor < ActiveRecord::Base
  has_many :promo_codes  # Zero or more.
end

# app/models/promo_code.rb
#table has sponsor_id field
class PromoCode < ActiveRecord::Base
  belongs_to :sponsor  # Zero or one.
end

has_one isn't appropriate here, as it would replace has_many: ie, you either have "has_many" and "belongs_to" OR "has_one" and "belongs_to". has_one isn't generally used much: usually it is used when you already have a has_many relationship that you want to change to has_one, and don't want to restructure the existing tables.

like image 29
Max Williams Avatar answered Oct 17 '22 15:10

Max Williams


In Rails 5, belongs_to is defined as required by default. To make it optional use the 'optional' option :)

class User
  belongs_to :company, optional: true
end

Source: https://github.com/rails/rails/issues/18233

like image 180
Paul Ardeleanu Avatar answered Oct 17 '22 15:10

Paul Ardeleanu


Unless you specify validation, relationships are optional by default.

The belongs_to is to tell rails the other half of the relationship between those two objects so you can also call @promo_code.sponsor and, vice versa, @sponsor.promo_codes.

like image 1
maxshelley Avatar answered Oct 17 '22 14:10

maxshelley