Have a quick question about the has_many
and belongs to
. I'm in a situation where a :user has_many :accounts
, and an :account belongs_to the user
.
In my controller, I first assign @accounts = current_user.accounts
. Debugging [correctly] reports to me that this particular use has 2 accounts. In the next line, I save an invalid account (debugging also tells me correctly that it is invalid). However, when I inspect current_user.accounts.count
versus current_user.accounts.all.count
versus current_user.accounts.all(order: 'created_at DESC')
, I get the following values:
- current_user.accounts.count = 2
- current_user.accounts.all.count = 3
- current_user.accounts.all(order: 'created_at DESC') = 2
Inspection of the database confirms that the invalid model indeed did not get saved.
Furthermore, in my dynamically ajax re-loaded view to which I feed @accounts = current_user.accounts
(which is set after the if-else loop checking if @account.save
worked), it loops through and displays 3 accounts, including the invalid account.
Here's the code for the controller:
def create
@account = current_user.accounts.new(account_params)
if @account.save
@accounts = current_user.accounts.all(order: 'created_at DESC')
#redirect_to accounts_path, :success => "Account successfully created."
flash[:success] = "Account sucessfully created."
# respond_to :js
respond_to do |format|
format.js {
render :create
}
end
else
@accounts = current_user.accounts.all(order: 'created_at DESC')
flash[:error] = "There was a problem with adding your account."
respond_to do |format|
format.js {
render :create
}
end
end
puts "Final Accounts is #{@accounts.count} compared to #{current_user.accounts.all.count} compared to #{current_user.accounts.count}" # outputs 2, 3, and 2 for an invalid model being saved
end
Can someone explain to me the proper way I should be doing this? Or better yet, what is going on under the Rails engine? I feel sheepishly noob for having this issue.
How do I tell rails to only load the current_user.accounts
that are saved in the db? Is this eager-loading related?
I'm running on Rails 4, with postgresql if this makes a difference.