Chris,
I don''t believe there is a way to specify an association condition.
You''ll need to create a custom validation method.
If you don''t envision the roles changing that much, you could also use
single table inheritance.
You could have a base User class that doesn''t have the items
associations, and then define a subclass that does have the items
association.
Cheers,
Derek
On 9/23/05, Chris Hall
<christopher.k.hall-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I have my database setup as follows (simplified for example):
>
> table: users
> id
> role_id
> ....
>
> table: roles
> id
> ....
>
> table: items
> id
> user_id
> .....
>
>
> models are:
>
> class User < ActiveRecord::Base
> belongs_to :role
> has_many :items
> ....
> end
>
> class Role < ActiveRecord::Base
> has_many :users
> ....
> end
>
> class Item < ActiveRecord::Base
> belongs_to :user
> ....
> end
>
> from this, you can obviously see that items are ''owned''
by
> users....and users belong to a particular role. Now, my question
> is...how would I enforce a relationship of some sort where items can
> only owned by a user which belongs to a particular role? Can this be
> done in the model association? or would this have to be enforced in
> the business logic of the model?
>
> something like belongs_to :users, :conditions => "role_id = 1"
>
> Chris
> _______________________________________________
> Rails mailing list
> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Derek Haynes
HighGroove Studios - http://www.highgroove.com
Keeping it Simple.
404.593.4879