bullet
bullet copied to clipboard
False positive with multiple nested attributes
Ruby Version: 2.4.1p111 Rails Version: 5.2.0
Having the following relations:
class User
has_one user_preference
has_one lifestyle
end
class UserPreference
belongs_to :user
has_many :goal_preferences, dependent: :destroy
has_many :goals, through: :goal_preferences
end
class Lifestyle
belongs_to :user
has_many :food_type_lifestyles, dependent: :destroy
has_many :food_types, through: :food_type_lifestyles
has_many :activity_type_lifestyles, dependent: :destroy
has_many :activity_types, through: :activity_type_lifestyles
end
Bullet is warning about N+1 in User => Lifestyle
when creating a User
.
I had the nested attributes for UserPreference
before and everything was ok. Then I added Lifestyle
and the warning started to appear.
Important: It only happens when I send both nested attributes in the params, it turns out that if I send only user_preference_attributes
or lifestyle_attributes
it won't complain.
Example params:
{
"user":
{
"email": "[email protected]",
"lifestyle_attributes": {
"food_type_ids": [1, 2],
"activity_type_ids": [1, 2, 3]
},
"user_preference_attributes": {
"goal_ids": [1, 2, 3, 4, 5]
}
}
}
Note: All the INSERT
queries for the many-to-many relations are executed in a single transaction, so the nested attributes behavior is fine.