12
The Type of Associations Ruby On Rails (By Nand Kishore)

Ruby association

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ruby association

The Type of AssociationsRuby On Rails

(By Nand Kishore)

Page 2: Ruby association

Types

● has_one● belongs_to● has_many● has_many :through● has_and_belongs_to_many● polymorphic association

Page 3: Ruby association

The has_one Association

● A has_one association also sets up a one-to-one connection with another model.

● Use has_one in the base, and belongs_to in the associated model.

class Supplier < ActiveRecord::Base has_one :accountendclass Account < ActiveRecord::Base belongs_to :supplierend

Page 4: Ruby association

The belongs_to Association

● A belongs_to association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model

● Use has_one in the base, and belongs_to in the associated model.

● Use has_many in the base, and belongs_to in the associated model.

Page 5: Ruby association

The has_many Association

● A has_many association indicates a one-to-many connection with another model.

● You’ll often find this association on the “other side” of a belongs_to association.

● This association indicates that each instance of the model has zero or more instances of another model.class Post < ActiveRecord::Base has_many :tagsendclass Tag < ActiveRecord::Base belongs_to :post # foreign key - post_idend

Page 6: Ruby association

Nested Form

<%= f.fields_for :tags do |b| %> <div class="field"> <%= b.label :name, "Tag" %><br /> <%= b.text_field :name %> </div> <% end %>

Modelhas_many :tagsaccepts_nested_attributes_for :tags, :allow_destroy => true

Page 7: Ruby association

The has_many :through Association

● A has_many :through association is often used to set up a many-to-many connection with another model.

● This association indicates that the declaring model can be matched with zero or more instances of another model by proceeding through a third model.

Page 8: Ruby association
Page 9: Ruby association

The has_and_belongs_to_many Association

● A has_and_belongs_to_many association creates a direct many-to-many connection with another model, with no intervening model.class Student < ActiveRecord::Base has_and_belongs_to_many :teachersend class Teacher < ActiveRecord::Base has_and_belongs_to_many :studentsend

Page 10: Ruby association

Choosing between has_many :thorugh and has_and_belongs_to_many

● The simpler way is to use has_and_belongs_to_many, which allows you to make the association directly:

● The second way to declare a many-to-many relationship is to use has_many :through. This makes the association indirectly, through a join model:

● You should use has_many :through if you need validations, callbacks, or extra attributes on the join model.

Page 11: Ruby association

The polymorphic Association

○ A slightly more advanced twist on associations is the polymorphic association. With polymorphic associations, a model can belong to more than one other model, on a single association. class Profile < ActiveRecord::Base

belongs_to :user, :polymorphic => trueendclass Student < ActiveRecord::Base has_one :profile, :as => :userendclass Teacher < ActiveRecord::Base has_one :profile, :as => :userend

Page 12: Ruby association

Thanks

Nand Kishore