Closed bbasanta238 closed 2 months ago
Thank you for reporting! This seems a bug, I'll investigate it.
@bbasanta238 OK, so the quick fix is to remove the second parameter of the block. As described in https://github.com/okuramasafumi/alba/blob/main/README.md#conditional-attributes, the second parameter is for the attribute, in this case, posts. After removing it, Alba understands the attribute is not necessary.
You can access params
in the block without parameters.
I added more detailed explanation to README: https://github.com/okuramasafumi/alba?tab=readme-ov-file#caution-for-the-second-parameter-in-if-proc
@okuramasafumi Thank you. Removing the second parameter works as expected. But suppose i need only uid
and email
attribute but it is executing query to select all data.
SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
Is it supposed to be like that or it can be optimized? And what about eager loading?
@bbasanta238 First of all, ActiveRecord queries all columns by default. Also, Alba is not designed specifically for Rails/ActiveRecord, and it's not planned to add such optimizations to Alba. You can do it by yourself outside of Alba, for example:
users = User.select(:uid, :email)
UserSerializer.new(users).serialize
I'm closing it now since I believe it's resolved.
Describe the bug
When serializing a user object with conditional associations using Alba, SQL queries are executed for associations even when the condition for including those associations is set to false.
To Reproduce
Steps to reproduce the behavior:
User
model with associations likePost
.Expected behavior
SQL queries for associations should not be executed when the condition for including those associations is set to false.
Actual behavior
SQL queries for associations are executed regardless of the conditional inclusion settings.
Environment