Carsten Gehling pisze:
Piotr Nowak wrote:
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.
You have a nice evening too.
- Carsten
ok ive done this, but when i do
usr = User.find(:first)
usr.groups
i get error:
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the associa
tion :membership in model User
my models and tables are as follows
class User < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :membership
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
class Group < ActiveRecord::Base
has_many :memberships, :dependent => :destroy
has_many :users, :through => :membership
end
excerpt form my schema.rb:
create_table "memberships", :force => true do |t|
t.column "group_id", :integer
t.column "user_id", :integer
end
any ideas ?