| Module | Her::Model::Associations::ClassMethods |
| In: |
lib/her/model/associations.rb
|
Return @_her_associations, lazily initialized with copy of the superclass’ her_associations, or an empty hash.
@private
Define a belongs_to association.
@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data @option opts [Symbol] :foreign_key The foreign key used to build the `:id` part of the path (defaults to `{name}_id`)
@example
class User
include Her::Model
belongs_to :team, :class_name => "Group"
end
class Group
include Her::Model
end
@user = User.find(1) # => #<User(users/1) id=1 team_id=2 name="Tobias">
@user.team # => #<Team(teams/2) id=2 name="Developers">
# Fetched via GET "/teams/2"
Define an has_many association.
@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)
@example
class User
include Her::Model
has_many :articles
end
class Article
include Her::Model
end
@user = User.find(1)
@user.articles # => [#<Article(articles/2) id=2 title="Hello world.">]
# Fetched via GET "/users/1/articles"
Define an has_one association.
@param [Symbol] name The name of the method added to resources @param [Hash] opts Options @option opts [String] :class_name The name of the class to map objects to @option opts [Symbol] :data_key The attribute where the data is stored @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)
@example
class User
include Her::Model
has_one :organization
end
class Organization
include Her::Model
end
@user = User.find(1)
@user.organization # => #<Organization(organizations/2) id=2 name="Foobar Inc.">
# Fetched via GET "/users/1/organization"