Class: Her::Model::Associations::Association

Inherits:
Object
  • Object
show all
Defined in:
lib/her/model/associations/association.rb

Instance Method Summary collapse

Instance Method Details

#find(id) ⇒ Object

Fetches the data specified by id

Examples:

class User
  include Her::Model
  has_many :comments
end

user = User.find(1)
user.comments.find(3) # Fetched via GET "/users/1/comments/3


97
98
99
100
101
# File 'lib/her/model/associations/association.rb', line 97

def find(id)
  return nil if id.blank?
  path = build_association_path -> { "#{@parent.request_path(@params)}#{@opts[:path]}/#{id}" }
  @klass.get_resource(path, @params)
end

#reloadObject

Refetches the association and puts the proxy back in its initial state, which is unloaded. Cached associations are cleared.

Examples:

class User
  include Her::Model
  has_many :comments
end

class Comment
  include Her::Model
end

user = User.find(1)
user.comments = [#<Comment(comments/2) id=2 body="Hello!">]
user.comments.first.id = "Oops"
user.comments.reload # => [#<Comment(comments/2) id=2 body="Hello!">]
# Fetched again via GET "/users/1/comments"


121
122
123
124
# File 'lib/her/model/associations/association.rb', line 121

def reload
  reset
  fetch
end

#where(params = {}) ⇒ Object Also known as: all

Add query parameters to the HTTP request performed to fetch the data

Examples:

class User
  include Her::Model
  has_many :comments
end

user = User.find(1)
user.comments.where(:approved => 1) # Fetched via GET "/users/1/comments?approved=1


81
82
83
84
# File 'lib/her/model/associations/association.rb', line 81

def where(params = {})
  return self if params.blank? && @parent.attributes[@name].blank?
  AssociationProxy.new clone.tap { |a| a.params = a.params.merge(params) }
end