Skip to content

Is there any way to use active record relations? #238

Answered by okuramasafumi
akira-19 asked this question in Q&A
Discussion options

You must be logged in to vote

@akira-19 You can do so by splitting attributes into mandatory ones and optional ones and using if option.

class UserResource
  include Alba::Resource
  
  attributes :id, :email
  many :posts, resource: PostResource
end

class PostResource
  include Alba::Resource
  
  attributes :id, if: { params.dig(:posts, :include_id) != false }
  attributes :content
end

UserResource.new(user, params: {posts: {include_id: false})

Could you try this approach and see how it goes?

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@akira-19
Comment options

@okuramasafumi
Comment options

Answer selected by okuramasafumi
@akira-19
Comment options

@akira-19
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants