I don't know your model, but is there a reason why a member and a user
have to be different tables/classes? (I'm going by the descriptions
of your data relations)
For lack of more information, it appears to me there is something
wrong with your data model, like maybe you are trying to shoehorn what
you consider unstable identity (user) into stable identity (member).
I don't see that as a useful relational set up. What's your use
case?.
I think most database guys would agree that one to one relations are
simple tuples. Why would you be required to make them different
tables/classes? From your original post, a member _is_ a user. The
only thing I can think of why you would want to separate them is
because that relation changes over time, which can easily be handled
by the db (think "create table user_update...").
Todd
···
On Dec 16, 2007 1:45 PM, John Griffiths <indiehead@gmail.com> wrote:
Well, if your model is what I think it is, a team doesn't have one
member. A user doesn't have one member either. Both a team and a
user have zero or more "memberships". You can restrict the
membershipness to exactly one for the user in the database if you want
to with check constraints. (or you could just have 2 tables, team and
member). How is this multiple<->multiple handled in Rails? I think
it's handled like this (using ActiveRecord with the necessary table
names and primary key names, of course):
class Person < ActiveRecord::Base
has_many :members
has_many :teams, :through => :members
end
class Member < ActiveRecord::Base
has_one :person
has_one :team
end
class Team < ActiveRecord::Base
has_many :members
has_many :persons, :through => :members
end
not tested,
Todd
···
On Dec 16, 2007 1:45 PM, John Griffiths <indiehead@gmail.com> wrote:
Well, I certainly must apologize for that rant. I think I misread
what you said. It's just that I've seen a lot of models similar to
yours that were a nightmare to work with when doing reports; that's
all.
<bows out and tries not to get killed on the way out>
Todd
···
On Dec 16, 2007 3:48 PM, Todd Benson <caduceass@gmail.com> wrote:
On Dec 16, 2007 1:45 PM, John Griffiths <indiehead@gmail.com> wrote:
> thanks, sort of works.
>
> isn't it possible to do:
>
> has_one :member
> has_one :team, :through => member
>
> every time i try i get...
>
> undefined key(s) through
I don't know your model, but is there a reason why a member and a user
have to be different tables/classes? (I'm going by the descriptions
of your data relations)
For lack of more information, it appears to me there is something
wrong with your data model, like maybe you are trying to shoehorn what
you consider unstable identity (user) into stable identity (member).
I don't see that as a useful relational set up. What's your use
case?.
I think most database guys would agree that one to one relations are
simple tuples. Why would you be required to make them different
tables/classes? From your original post, a member _is_ a user. The
only thing I can think of why you would want to separate them is
because that relation changes over time, which can easily be handled
by the db (think "create table user_update...").
ordinarily you'd be spot on, but in this case each user can only be
associated to one team and that team can have only two or less members
(users).
I think with the unusual model you have (most users often are part of
many teams) you may be having a harder time just figuring out how the
relationships are in actuality simply for the fact that our minds tell
us something doesn't feel right because we know users often want to be
able to be on multiple teams.
We get mental friction when we know we're doing things wrong or a way
our mind doesn't exactly recognize as normal.
Not saying that you are, just say that it's possible that there could
be a better way to define the relationships.
With that said, if users really only belong to one team, then why
bother with memberships in the first place? You could easily have just
two tables and models if a user ever only has one team. (Though, like
I said, that's not usually the case).