Tutorial by Examples

A belongs_to association sets up a one-to-one connection with another model, so each instance of the declaring model "belongs to" one instance of the other model. For example, if your application includes users and posts, and each post can be assigned to exactly one user, you'd declare th...
A has_one association sets up a one-to-one connection with another model, but with different semantics. This association indicates that each instance of a model contains or possesses one instance of another model. For example, if each user in your application has only one account, you'd declare the...
A has_many association indicates a one-to-many connection with another model. This association generally is located 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. For example, in an application ...
This type of association allows an ActiveRecord model to belong to more than one kind of model record. Common example: class Human < ActiveRecord::Base has_one :address, :as => :addressable end class Company < ActiveRecord::Base has_one :address, :as => :addressable end cl...
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. For example, consider a medical practice where p...
A has_one :through association sets up a one-to-one connection with another model. This association indicates that the declaring model can be matched with one instance of another model by proceeding through a third model. For example, if each supplier has one account, and each account is associated...
A has_and_belongs_to_many association creates a direct many-to-many connection with another model, with no intervening model. For example, if your application includes assemblies and parts, with each assembly having many parts and each part appearing in many assemblies, you could declare the models...
Self-referential association is used to associate a model with itself. The most frequent example would be, to manage association between a friend and his follower. ex. rails g model friendship user_id:references friend_id:integer now you can associate models like; class User < ActiveRecord:...

Page 1 of 1