Help with relations

Hello

I have the three database models: "seller", "client" and "account". A seller may have many clients and many accounts, but I would like to add the constraint that given a seller and a client, only one account may exist between them.

Is there a way to express this contraint using ActiveRecord relations?

Thanks in advance, Andre

andrenth@gmail.com wrote:

Hello

I have the three database models: "seller", "client" and "account". A seller may have many clients and many accounts, but I would like to add the constraint that given a seller and a client, only one account may exist between them.

Is there a way to express this contraint using ActiveRecord relations?

Yes -- validates_uniqueness_of will do the trick -- but never rely solely on ActiveRecord constraints. You need a unique index (and foreign key constraints) on the DB side too.

Thanks in advance, Andre

Best,

I am already using the validations and key constrains. I was actually wondering about the possibility of having Active Record generate helper methods reflecting that kind of relation.

Thanks, Andre

Does Client belongs_to seller, and Account belongs_to seller? If so then you could put client and account together in one table. Alternatively have Client has_one account and Account belongs_to client, or the other way round. This doesn't work if you have HABTM relationship of course.

Colin