Ruby association

763 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
763
On SlideShare
0
From Embeds
0
Number of Embeds
8
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Ruby association

  1. 1. The Type of Associations Ruby On Rails (By Nand Kishore)
  2. 2. Types● has_one● belongs_to● has_many● has_many :through● has_and_belongs_to_many● polymorphic association
  3. 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 :accountendclass Account < ActiveRecord::Base belongs_to :supplierend
  4. 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. 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. 6. 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
  7. 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. 8. 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
  9. 9. Choosing between has_many :thorugh andhas_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.
  10. 10. 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
  11. 11. ThanksNand Kishore

×