her icon indicating copy to clipboard operation
her copied to clipboard

Allow scopes on associations

Open forest opened this issue 11 years ago • 1 comments

I expected scopes on association to work, but they currently don't. Is this a planned feature, or something that is worth me investigating as an enhancement?

Any direction would be appreciated if this enhancement is desired for the project.

class Project
  include Her::Model

  has_many :iterations

  # This works.
  def current_iteration
    iterations.where(scope: :current).first
  end
end

class Iteration
  include Her::Model

  belongs_to :project
  has_many :stories

  scope :done, -> { where(scope: :done) }
  scope :current, -> { where(scope: :current) }
  scope :offset, ->(offset) { where(offset: offset) }
end

project = Project.find(12345)
iteration = project.current_iteration

This is what I want to be able to do, but current is undefined on the Her::Model::Associations::HasManyAssociation

iteration = project.iterations.current
iteration = project.iterations.done.offset(-5)

forest avatar Feb 20 '14 19:02 forest

This is something that would be really useful. This involves rewriting the associations code to make it compatible with the Her::Model::Relation system.

Let me know if this something you’d like to work on! :smile:

remi avatar Jun 10 '14 00:06 remi