has_many association order – or, association scopes? #115

Closed
mbrock opened this Issue Jan 9, 2015 · 8 comments

Comments

Projects
None yet
2 participants
@mbrock
Contributor

mbrock commented Jan 9, 2015

While porting the dummy test app of rails_admin, I ran into a discrepancy with Mongoid: namely, how to port the following.

class Team
  include Mongoid::Document
  has_many :players, order: :_id.desc
end

What do you think about supporting scopes on has_many associations? For example:

class Team
  include Mongoid::Document
  has_many :players { order(id: :desc) }
end

I'm not very familiar with the NoBrainer internals, but I had a hunch that with the recent change to support chained scopes (fc88622), it might be easy?

@mbrock

This comment has been minimized.

Show comment
Hide comment
@mbrock

mbrock Jan 9, 2015

Contributor

(This might be a far-fetched idea; just allowing an order parameter seems reasonable, too.)

Contributor

mbrock commented Jan 9, 2015

(This might be a far-fetched idea; just allowing an order parameter seems reasonable, too.)

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 9, 2015

Owner

Yeah I can do that, but it might not be as trivial as it sound, we'll see.

The API will look like has_many :xxx, :scope => where(xxx)

Owner

nviennot commented Jan 9, 2015

Yeah I can do that, but it might not be as trivial as it sound, we'll see.

The API will look like has_many :xxx, :scope => where(xxx)

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 9, 2015

Owner

However, when using a scope, you don't be able to use :dependent => :destroy, otherwise, it's not obvious what should happen.

Owner

nviennot commented Jan 9, 2015

However, when using a scope, you don't be able to use :dependent => :destroy, otherwise, it's not obvious what should happen.

@mbrock

This comment has been minimized.

Show comment
Hide comment
@mbrock

mbrock Jan 9, 2015

Contributor

Fair enough. I would be happy with a simple order option on has_many, but scoped associations seems like a pretty cool feature.

Maybe as a future feature, there could be two other options for dependent, something like :destroy_all and :destroy_scoped?

Contributor

mbrock commented Jan 9, 2015

Fair enough. I would be happy with a simple order option on has_many, but scoped associations seems like a pretty cool feature.

Maybe as a future feature, there could be two other options for dependent, something like :destroy_all and :destroy_scoped?

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 9, 2015

Owner

Feels like unnecessary complications. I don't see why you would want to destroy only a subset of your association. For weird cases like this, one can always do a before_destroy { my_weird_scope.destroy_all } or whatever

Owner

nviennot commented Jan 9, 2015

Feels like unnecessary complications. I don't see why you would want to destroy only a subset of your association. For weird cases like this, one can always do a before_destroy { my_weird_scope.destroy_all } or whatever

@mbrock

This comment has been minimized.

Show comment
Hide comment
@mbrock

mbrock Jan 9, 2015

Contributor

Agreed.

Contributor

mbrock commented Jan 9, 2015

Agreed.

@nviennot nviennot closed this in c8005e1 Jan 9, 2015

@nviennot

This comment has been minimized.

Show comment
Hide comment
@nviennot

nviennot Jan 9, 2015

Owner

All done. Note that you can only pass lambdas to the scope option. A bare where won't work.
This lambda will be evaluated in the context of the target model, so you can use its named scopes.
Example:

class Post
  has_many :admin_comments, :class_name => 'Comment', :scope => ->{ from_admin }
end

class Comments
  belongs_to :post

  # we could also use the scope keyword
  def self.from_admin
    where(:admin_author => true)
  end
end
Owner

nviennot commented Jan 9, 2015

All done. Note that you can only pass lambdas to the scope option. A bare where won't work.
This lambda will be evaluated in the context of the target model, so you can use its named scopes.
Example:

class Post
  has_many :admin_comments, :class_name => 'Comment', :scope => ->{ from_admin }
end

class Comments
  belongs_to :post

  # we could also use the scope keyword
  def self.from_admin
    where(:admin_author => true)
  end
end
@mbrock

This comment has been minimized.

Show comment
Hide comment
@mbrock

mbrock Jan 9, 2015

Contributor

Very cool! :)
fre 9 jan 2015 kl. 18:16 skrev Nicolas Viennot notifications@github.com:

All done. Note that you can only pass lambdas to the scope option. A bare
where won't work.
This lambda will be evaluated in the context of the target model, so you
can use its named scopes.
Example:

class Post
has_many :admin_comments, :class_name => 'Comment', :scope => ->{ from_admin }end
class Comments
belongs_to :post

we could also use the scope keyword

def self.from_admin
where(:admin_author => true)
endend


Reply to this email directly or view it on GitHub
#115 (comment).

Contributor

mbrock commented Jan 9, 2015

Very cool! :)
fre 9 jan 2015 kl. 18:16 skrev Nicolas Viennot notifications@github.com:

All done. Note that you can only pass lambdas to the scope option. A bare
where won't work.
This lambda will be evaluated in the context of the target model, so you
can use its named scopes.
Example:

class Post
has_many :admin_comments, :class_name => 'Comment', :scope => ->{ from_admin }end
class Comments
belongs_to :post

we could also use the scope keyword

def self.from_admin
where(:admin_author => true)
endend


Reply to this email directly or view it on GitHub
#115 (comment).

nviennot added a commit that referenced this issue Jan 9, 2015

nviennot added a commit that referenced this issue Jan 10, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment