Skip to content
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

no object in Resource #10

Closed
zoran opened this issue Mar 2, 2017 · 6 comments
Closed

no object in Resource #10

zoran opened this issue Mar 2, 2017 · 6 comments

Comments

@zoran
Copy link

zoran commented Mar 2, 2017

in a more complex scenario I want to access attributes of the @object in a Resource (just like in a Serializer). Is that possible somehow?

class UserResource
  type :users
  
  has_many :addresses,
    resource: AdressResource,
    foreign_key: :abc,
    primary_key: :xyz,
    scope -> { Adress.where("zip LIKE ?", @object.zip) }
end
@richmolj
Copy link
Contributor

richmolj commented Mar 2, 2017

@zoran I think this is probably best left to the lower-level DSL, which you can customize with a block. We wouldn't be dealing with @object at this point but @objects (because you could be hitting an index endpoint listing all users, then want all addresses for all users). That's available in the lower-level DSL:

class UserResource
  type :users
  
  has_many :addresses, options do
    scope do |users|
      Address.where(["zip LIKE ?", users.map(&:zip))
    end
  end
end

Does that work for you? Note this will override any scope option you pass to has_many.

@zoran
Copy link
Author

zoran commented Mar 3, 2017

mhh, I get missing keywords: scope and I have to use an empty scope option:

has_many :addresses,
  scope: -> {} do
    scope do |users|
      Address.where(["zip LIKE ?", users.map(&:zip))
    end
  end

@richmolj
Copy link
Contributor

richmolj commented Mar 4, 2017

Yeah, I should definitely make it so you can pass EITHER scope key, OR a scope block. primary_key/foreign_key would still be used to assign the objects to each other, though.

After that I'd say the abstraction still looks correct to me. Would just making scope optional sound good to you?

@zoran
Copy link
Author

zoran commented Mar 6, 2017

Yes that would be great

richmolj pushed a commit that referenced this issue Mar 6, 2017
@richmolj
Copy link
Contributor

richmolj commented Mar 6, 2017

@zoran this is in master, I will release a new version tonight

@richmolj
Copy link
Contributor

richmolj commented Mar 6, 2017

Fixed in 0.5.5

@richmolj richmolj closed this as completed Mar 6, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants