We have a users table with accounts and patients tables, that both belong_to :user. We eager load the accounts and patients table for a given user record like this:
user = User.includes([:accounts, :patients]).find_by(email: "firstname.lastname@example.org")
user.accounts.create(:name => "test name", :customer => "test customer")
account1 = user.accounts.first
account1 = user.accounts.first user.accounts.destroy(account1)
It actually doesn't matter for your question, or the answer, that you eager-loaded the association. What matters is that the
user.accounts association has already been loaded (whether by eager-loading or ordinary on-demand loading), and you want to destroy one of the objects such that, like you say, it both removes it from the database and removes it from the already-loaded in-memory association.
You do that by using the
destroy method that any has-many association has.
destroy method on any has-many association, and others available on any has-many association, are briefly summarized in the Rails Guide, and in a little bit more details in the API docs for the has_many association.
user.accounts.destroy is sort of the inverse of
user.accounts.create you already knew about.
Another useful one is
<< method on a has-many association, which is like
create but with an already existing
Account object instead of a hash of attributes like
account = Account.new(:name => "test name", :customer => "test customer") user.accounts << account
...which both creates the new account object and adds it to the in-memory
There are a handful of other useful methods that are available on any has-many association, as well as slightly different sets of useful methods that go along with
has_one, etc. Worth reviewing the docs, linked above, to see what's available. (I forget about some of them sometimes, or remember they exist but forget the method signature and have to look it up).