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

Inherits:
Object
  • Object
show all
Defined in:
lib/castle-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


94
95
96
97
98
# File 'lib/castle-her/model/associations/association.rb', line 94

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


78
79
80
81
# File 'lib/castle-her/model/associations/association.rb', line 78

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