Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Home

winton edited this page · 8 revisions
Clone this wiki locally

Model

class User < ActiveRecord::Base
  acts_as_relationable :books
end

class Book < ActiveRecord::Base
  acts_as_relationable :users
end

Controller

b = Book.find :first
u = User.find :first
u.books << b
u.books	# [ b ]
b.users	# [ u ]

h2. Self referential relationships

class User < ActiveRecord::Base
  acts_as_relationable :users
end

u = User.create
self.child_users << u
u.parent_users	# == self

Relationship-specific fields

Say you want users to be able to ‘friend’ and ‘best friend’ each other.

Run the generator

script/generate acts_as_relationable friend:boolean best_friend:boolean
rake db:migrate

Model

class User < ActiveRecord::Base
  acts_as_relationable :users, :fields => [ :friend, :best_friend ]

  self.child_users.first.update_attribute :friend, true
  self.child_users.first.friend?        # == true
  self.child_users.first.best_friend?   # == false
  self.child_users.friends              # [ self.child_users.first ]
end
Something went wrong with that request. Please try again.