Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10518 (closed defect: fixed)

Opened 7 months ago

Last modified 2 months ago

[PATCH] save should fails for invalid has_one association

Reported by: lifofifo Assigned to: core
Priority: normal Milestone: 2.x
Component: ActiveRecord Version: edge
Severity: normal Keywords:
Cc:

Description

When you call save() on parent object which has an invalid has_one association, save() doesn't fail.

firm = Firm.find(:first)
firm.account = Account.new # Associate INVALID account
firm.save # This doesn't fail

The patch fixes the issue.

Attachments

fix_save_for_invalid_has_one.patch (2.0 kB) - added by lifofifo on 12/16/07 04:22:22.
updated_invalid_has_one_patch.patch (2.0 kB) - added by rubyruy on 04/06/08 02:22:12.

Change History

12/16/07 04:10:04 changed by chuyeow

+1. This is good for consistency with has_many's behavior.

For clarity in the test, perhaps something like:

firm.account = a = Account.new
assert !account.valid?

?

12/16/07 04:21:57 changed by lifofifo

Yeah. Good idea. Updated the patch.

12/16/07 04:22:22 changed by lifofifo

  • attachment fix_save_for_invalid_has_one.patch added.

12/18/07 21:20:08 changed by revans

+1

04/06/08 02:12:02 changed by rubyruy

+1 tested in postgresql and sqlite3 also attached updated patch since the source of this is pretty old

04/06/08 02:22:12 changed by rubyruy

  • attachment updated_invalid_has_one_patch.patch added.

04/06/08 02:22:28 changed by rubyruy

previous patch was slightly malformed

04/06/08 02:32:54 changed by pratik

  • status changed from new to closed.
  • resolution set to fixed.

(In [9232]) Ensure that save on parent object fails for invalid has_one association. Closes #10518. [Pratik]

05/15/08 12:43:35 changed by theflow

Just testing the RC and I have a problem because of this change:

I'm offloading some data to a second model to make the DB table smaller:

class User
  has_one :info
  after_save { |u| u.info.save if u.info }
end

class Info
  belongs_to :user
  validates_presence_of :user
end

So now I run into a catch-22 because I can't save the user because the info association is not valid and I can't save the info because it needs a user_id.

Is this a bug? Or is there a better way to do what I'm currently doing