i use models User and Groups connected with many to many association, so
i have users table, groups and groups_users.
lets say i create one user and one group. next i add a group to user. so
now my table groups_users has one entry i.e.
group_id | user_id
_|
1 | 1
ok, when i delete both my group and user, above entry is not deleted,
thus leaving alone without any reason… its just an orphan record, which
is useless.
is there any parameter in rails that removes such records ?
i dont want to have too many useless records at all.
btw. its not :dependant => :destroy parameter, this applies only to
has_many association…
You ought to be able to do this on your existing database and
application without changing any other program lines. The access to the
associated data are the same. And the :dependant declaration will take
care of future orphans.
btw. i wonder why something simple isnt done with regular habtm
relationship…
I can’t remember when the :dependent keyword was added to ActiveRecord,
but I know, that ever since Rails 1.2, “has_many :trough” has been
favored by the Rails core team over HABTM.
BTW: “has_many :through” has another advantage over HABTM: In your
“memberships” table, you are allowed to add attributes e.g. like this
memberships
id (int)
user_id (int)
group_id (int)
valid_from (datetime)
valid_to (datetime)
and access these from your model code.
anyway, thanks a lot Carsten
have a nice day (evening)
You are very welcome, I am (unfortunately) not often on the Ruby-forum,
but it gives me pleasure when I’m able to help others in need.
well, although it works, it doesnt resolve my problem
when i delete user or group, the membership item (containng both those
user and group) is not deleted…
well, although it works, it doesnt resolve my problem
when i delete user or group, the membership item (containng both those
user and group) is not deleted…
wonder why…
just noticed that it works only when i do destroy method on my user or
group instead of delete…
just noticed that it works only when i do destroy method on my user or
group instead of delete…
From the API description of ActiveRecord:
delete(id)
Deletes the record with the given id without instantiating an object
first. If an array of ids is provided, all of them are deleted.
destroy(id)
Destroys the record with the given id by instantiating the object and
calling destroy (all the callbacks are the triggered). If an array of
ids is provided, all of them are destroyed.
The essential here is that delete(id) “deletes without instantiating an
object first”. Which really boils down to a SQL like “delete from users
where id = NN limit 1”
delete() does not instantiate the object, therefore it knows nothing of
any associations/relationships.
Start with the ones in the bottom - they are fun and - IMHO quite
accurate
Been at a few of the langauges/frameworks myself earlier. Now I only do
PHP and Rails - and PHP only because I have so many existing
customers/solutions that I would hate to rewrite.
But you’ve made a good choice - you won’t regret it!
Start with the ones in the bottom - they are fun and - IMHO quite
accurate
Been at a few of the langauges/frameworks myself earlier. Now I only do
PHP and Rails - and PHP only because I have so many existing
customers/solutions that I would hate to rewrite.
But you’ve made a good choice - you won’t regret it!
Carsten
yes, ive seen them all, pretty funny, especially rails vs .net #2
starting 2 projects now in rails, im quite excited
greets.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.