Hello all, I''m fairly new to this so please excuse me if this sounds like a basic question. Reading the docs on AR I see that I can create assosiations in my models but does this mean AR maintains data integrity? Say, if I had: class Project < ActiveRecord::Base belongs_to :portfolio has_one :project_manager has_many :milestones has_and_belongs_to_many :categories end ...would AR errase a project''s milestones if I errased the project that owns them? Thank you. LG
Luis G. Gómez wrote:> Say, if I had: > > class Project < ActiveRecord::Base > belongs_to :portfolio > has_one :project_manager > has_many :milestones > has_and_belongs_to_many :categories > end > > ...would AR errase a project''s milestones if I errased the project that > owns them?It can, but you need to specify that milestones are dependent on the project: has_many :milestones, :dependent => true -- Marten Veldthuis
On Fri, Nov 19, 2004 at 09:16:26PM +0100, Marten Veldthuis wrote:> Luis G. G?mez wrote: > >Say, if I had: > > > >class Project < ActiveRecord::Base > > belongs_to :portfolio > > has_one :project_manager > > has_many :milestones > > has_and_belongs_to_many :categories > >end > > > >...would AR errase a project''s milestones if I errased the project that > >owns them? > > It can, but you need to specify that milestones are dependent on the > project: > > has_many :milestones, :dependent => trueThe docs for association methods are here: http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html marcel -- Marcel Molina Jr. <marcel-WRrfy3IlpWYdnm+yROfE0A@public.gmane.org>
Great! Thankk you. Marcel Molina Jr. wrote:> On Fri, Nov 19, 2004 at 09:16:26PM +0100, Marten Veldthuis wrote: > >>Luis G. G?mez wrote: >> >>>Say, if I had: >>> >>>class Project < ActiveRecord::Base >>> belongs_to :portfolio >>> has_one :project_manager >>> has_many :milestones >>> has_and_belongs_to_many :categories >>>end >>> >>>...would AR errase a project''s milestones if I errased the project that >>>owns them? >> >>It can, but you need to specify that milestones are dependent on the >>project: >> >> has_many :milestones, :dependent => true > > > The docs for association methods are here: > http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html > > marcel
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Luis G. Gómez wrote: | ...would AR errase a project''s milestones if I errased the project that | owns them? has_many and has_one use the :dependent option to specify whether you want associated objects to be destroyed along with the parent object. The default is false. class Project < ActiveRecord::Base ~ has_many :milestones, :dependent => true end will destroy associated milestones when the project is destroyed. See the has_many options at http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#M000006 for more documentation. Best, jeremy -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.6 (Darwin) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFBnlYwAQHALep9HFYRAo+8AJ9IWgirzgel01uUgzvLcwFPaAARkgCfXUol 2pzSWazC2E/LSsWO5CqLUTA=//Im -----END PGP SIGNATURE-----