I have three models, two of them are linked through a join model:
# user.rb
has_many :user_cards
has_many :cards, through: :user_cards
# cards.rb
has_many :user_cards
has_many :users, through: user_cards
# user_cards.rb
belongs_to :user
belongs_to :card
When I want to add a card to a user, I want to specify an attribute of a UserCard, that's why I create it this way:
# example
user.user_cards.create(card: card, paid: true)
user.cards it doesn't contain my new record. user.user_cards it does contain my new record. I need to call user.cards.reset (or reload). I don't like this solution because it requires another query.
Do you know how can I have reload|refresh|update user.cards without an additional query?
Thank you!
Use user.cards(true) or user.cards(force_reload = true).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With