HasAssociations

ActiveRecord association helpers

Installation

Add this line to your application's Gemfile:

gem 'has_associations'

And then execute:

$ bundle

Or install it yourself as:

$ gem install has_associations

Method Overview

Refer to these ActiveRecord models for overview examples

class Grandfather < ActiveRecord::Base
  has_many :fathers
  has_many :sons, through: :father
  has_association_helpers :sons
end

class Father < ActiveRecord::Base
  has_many :sons
  belongs_to :grandfather
  has_association_helpers
end

class Son < ActiveRecord::Base
  belongs_to :father
  has_one :grandfather, through: :father
  has_association_helpers
end

::has_association_helpers(*associations)

Creates has_(assoc)? helpers for associations

grandfather = Grandfather.create
father = Father.create :grandfather => grandfather
son = Son.create :father => father

grandfather.has_son? son
# => true
grandfather.has_father? father
# => NameError: undefined local variable or method `has_father?`

::has_association?(AR), ::belongs_to?(AR), ::has_many?(AR), ::has_one?(AR)

Verifies the ActiveRecord association exists

Son.belongs_to? Father # true
Father.has_many? Son # true
Son.has_one? Father # false
Son.has_one? Grandfather # true

::has_associations(names_with_macro = nil)

Gathers the ActiveRecords associations

  • names_with_macro: Used to return only the names of associations. If Symbol, returns names of associations with macro (ie. :has_many or :belongs_to). If true, returns names of all associations. If nil or false, returns all AssociationReflection(s)
Father.has_associations
# => [#<AssociationReflection @name=:grandfather, @macro=:belongs_to>, #<AssociationReflection @name=:sons, @macro=:has_many>]
Father.has_associations :has_many
# => [:sons]
Father.has_associations true
# => [:grandfather, :sons]

#has_(assoc)?(record)

Verifies the current ActiveRecord has the specified relationship with record; returns nil if relationship does not exist. Method(s) created with has_association_helpers

bob = Grandfather.create :name => 'Jerry'
dave = Father.create :name => 'Dave', :grandfather => bob
james = Son.create :name => 'James', :father => dave

bob.has_son? james # true
bob.has_father? dave # NameError: undefined local variable or method `has_father?`
dave.has_son? james # true
james.has_father? dave # true
james.has_father? bob # nil

Example Usage

class Sport < ActiveRecord::Base
  has_many :teams
  has_association_helpers
end

class Team < ActiveRecord::Base
  belongs_to :sport
  has_association_helpers
end

Sport.has_many? Team # true
Team.belongs_to? Sport # true
Sport.has_one? Team # false

strikers = Team.create :name => 'Strikers' # #<Team id: 1, name: 'Strikers'>
soccer = Sport.create :name => 'Soccer' # #<Soccer id: 1, name: 'Soccer'>

soccer.has_team? strikers # false
strikers.has_sport? soccer # false

strikers.update_attributes :sport => soccer

soccer.has_team? strikers # true
strikers.has_sport? soccer # true

Contributing

  1. Fork it ( http://github.com/bradynpoulsen/has_associations/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request