-
Notifications
You must be signed in to change notification settings - Fork 322
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scopes on associations #268
base: master
Are you sure you want to change the base?
Conversation
LGTM 👍 |
good job 👍 |
@@ -228,6 +236,11 @@ | |||
params[:comments].length.should eq(2) | |||
end | |||
|
|||
it 'supports scopes on assoications' do | |||
locked_comments = @user_with_included_data.comments.locked_scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i found this call will append another user_id
as parameter in the end..
/users/1/comments?locked=true&user_id=1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I'm not sure there is currently a good way to prevent that from happening. Any suggestions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fixed now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, confirmed the behavior 👍
…Take advantage of the fact when using scopes on associations to prevent the parent id from bing in the params.
I was hoping this would work for custom_get (which are kinda like server-side scopes) but it doesn't because of this bit of evilness in the scope method. # Add the scope method to the Relation class
Relation.instance_eval do
define_method(name) { |*args| instance_exec(*args, &code) }
end @remiprev, is this really necessary? Can't the relation fall back to the model's class methods in method_missing? I know ActiveRecord does this, though admittedly it is a bit messy when deciding whether methods like find should go to the collection or the model class. |
👍 |
@hubert Anything I can do to help get this merged? Also, thank you for stepping up to help maintain Her. |
@dturnerTS i've blocked out some time on saturday (tomorrow) to work through pending pull requests. thanks for the kind words. happy to contribute back to the gem. |
@edtjones, if you're looking at this one, I'd really appreciate having a chat with you about it. |
@chewi definitely, and looks really useful. Need to absorb fully. Are there any downsides / expected failure modes which mean we should be careful about merging this in? |
I'd like it to work more like it does in Active Record, where you can call any class method through an association. The code I quoted above highlights the difference between Her and Active Record. I'm a little hazy on the details after all this time but Active Record is very mature now and I think we should strive to use the same approaches wherever possible as they have clearly served it well. |
Any plans on merging this PR? |
hi @shashiprab @chewi this has been hanging around for ages - sorry! Don't have the mental bandwidth to deal with the implications of merging this at the moment but I would be delighted if someone else could review. |
Remember happily applying this patch a few years back and eventually moving a few things around. Also have a rough patch that would let us remove class User
include Her::Model
has_many :comments
end
class Comment
include Her::Model
belongs_to :user
scope :approved, -> { where(approved: true) }
end
> user = User.find(1)
# GET "/users/1"
> user.comments.approved
# GET "/users/1/comments?approved=true" I'll put a PR together soon. |
Punch a hole in the association_proxy for scopes. Also inject the parents id if needed.
Addresses #215