SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
The Type of Associations
        Ruby On Rails
      (By Nand Kishore)
Types
●   has_one
●   belongs_to
●   has_many
●   has_many :through
●   has_and_belongs_to_many
●   polymorphic 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 :account
end
class Account < ActiveRecord::Base
 belongs_to :supplier
end
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.
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 :tags
  end
  class Tag < ActiveRecord::Base
   belongs_to :post # foreign key - post_id
  end
Nested Form

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

Model
has_many :tags
accepts_nested_attributes_for :tags, :allow_destroy => true
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.
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 :teachers
  end

  class Teacher < ActiveRecord::Base
   has_and_belongs_to_many :students
  end
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.
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 => true
  end
  class Student < ActiveRecord::Base
   has_one :profile, :as => :user
  end
  class Teacher < ActiveRecord::Base
    has_one :profile, :as => :user
  end
Thanks

Nand Kishore

Weitere ähnliche Inhalte

Ähnlich wie Ruby association

FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fiv
FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fivFIXED INCOMEModule 3 Group Homework1. [6pts] Given a fiv
FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fivShainaBoling829
 
Software System Engineering - Chapter 9
Software System Engineering - Chapter 9Software System Engineering - Chapter 9
Software System Engineering - Chapter 9Fadhil Ismail
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UMLSwatiS-BA
 
UML Training for Business Analysts
UML Training for Business AnalystsUML Training for Business Analysts
UML Training for Business AnalystsSwatiS-BA
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopMohammad Shawahneh
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UMLShwetha-BA
 
Guide for building GLMS
Guide for building GLMSGuide for building GLMS
Guide for building GLMSAli T. Lotia
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classesSujit Kumar
 
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptx
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptxEN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptx
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptxZokhidKalonov
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guiceAman Verma
 
Aaa ped-19-Recommender Systems: Neighborhood-based Filtering
Aaa ped-19-Recommender Systems: Neighborhood-based FilteringAaa ped-19-Recommender Systems: Neighborhood-based Filtering
Aaa ped-19-Recommender Systems: Neighborhood-based FilteringAminaRepo
 

Ähnlich wie Ruby association (20)

Association in rails
Association in railsAssociation in rails
Association in rails
 
Software Design principales
Software Design principalesSoftware Design principales
Software Design principales
 
FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fiv
FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fivFIXED INCOMEModule 3 Group Homework1. [6pts] Given a fiv
FIXED INCOMEModule 3 Group Homework1. [6pts] Given a fiv
 
Software System Engineering - Chapter 9
Software System Engineering - Chapter 9Software System Engineering - Chapter 9
Software System Engineering - Chapter 9
 
Java- language Lecture 6
Java- language Lecture 6Java- language Lecture 6
Java- language Lecture 6
 
Template pattern
Template patternTemplate pattern
Template pattern
 
Agile cards
Agile cardsAgile cards
Agile cards
 
Introduction to UML
Introduction to UMLIntroduction to UML
Introduction to UML
 
UML Training for Business Analysts
UML Training for Business AnalystsUML Training for Business Analysts
UML Training for Business Analysts
 
Object Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshopObject Oriented, Design patterns and data modelling worshop
Object Oriented, Design patterns and data modelling worshop
 
Regresión
RegresiónRegresión
Regresión
 
Introduction to OOA and UML
Introduction to OOA and UMLIntroduction to OOA and UML
Introduction to OOA and UML
 
Classes2
Classes2Classes2
Classes2
 
Guide for building GLMS
Guide for building GLMSGuide for building GLMS
Guide for building GLMS
 
OO relationships between classes
OO relationships between classesOO relationships between classes
OO relationships between classes
 
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptx
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptxEN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptx
EN How to Find the Antiderivative of Simple Polynomials by Slidesgo.pptx
 
Dependency injection using Google guice
Dependency injection using Google guiceDependency injection using Google guice
Dependency injection using Google guice
 
Design pattern - part 1
Design pattern - part 1Design pattern - part 1
Design pattern - part 1
 
Aaa ped-19-Recommender Systems: Neighborhood-based Filtering
Aaa ped-19-Recommender Systems: Neighborhood-based FilteringAaa ped-19-Recommender Systems: Neighborhood-based Filtering
Aaa ped-19-Recommender Systems: Neighborhood-based Filtering
 
Design patterns
Design patternsDesign patterns
Design patterns
 

Ruby association

  • 1. The Type of Associations Ruby On Rails (By Nand Kishore)
  • 2. Types ● has_one ● belongs_to ● has_many ● has_many :through ● has_and_belongs_to_many ● polymorphic association
  • 3. 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 :account end class Account < ActiveRecord::Base belongs_to :supplier end
  • 4. 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.
  • 5. 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 :tags end class Tag < ActiveRecord::Base belongs_to :post # foreign key - post_id end
  • 6. Nested Form <%= f.fields_for :tags do |b| %> <div class="field"> <%= b.label :name, "Tag" %><br /> <%= b.text_field :name %> </div> <% end %> Model has_many :tags accepts_nested_attributes_for :tags, :allow_destroy => true
  • 7. 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.
  • 8.
  • 9. 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 :teachers end class Teacher < ActiveRecord::Base has_and_belongs_to_many :students end
  • 10. 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.
  • 11. 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 => true end class Student < ActiveRecord::Base has_one :profile, :as => :user end class Teacher < ActiveRecord::Base has_one :profile, :as => :user end