user4187589
user4187589

Reputation:

has_many :through, not reloaded/reset

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)

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!

Upvotes: 4

Views: 453

Answers (1)

tillmo
tillmo

Reputation: 616

Use user.cards(true) or user.cards(force_reload = true).

Upvotes: 2

Related Questions