I have two models called entity and user.
The entities table has a column called users_id that contains the user
id of the user that created the entity.
In entity I have…
has_one :user
… as I want to be able to show the user who created the entity from
the entity object.
But this produces the following error…
Mysql::Error: #42S22Unknown column ‘users.entity_id’ in ‘where clause’:
SELECT * FROM users WHERE (users.entity_id = 1) LIMIT 1
…Where I want it to select users by users.id.
What do I do? I bet it is easy.
Martin
On 1/31/06, linus1412 [email protected] wrote:
the entity object.
Martin
As a newbie I had a similar issue and I just added the belongs_to tag
to my entity model.
It definately feels wrong in my case to say that an entity belongs_to
a status, but it was the only thing I found to make the code work.
(Or I could directly code in a find() call).
I know that a One-to-Many relationship is mathematically equivalent to
a Many-to-One relationship but I personally think there should be a
belongs_to alias that makes sense for the Many-to-One case. Maybe
“Contains_one” or something similar.
ie. “Entity belongs_to status” is hard for my brain to parse. “Entity
contains_one status” makes much more sense. I’m fully aware the
underlying relationship is the same so this is simply a matter of
having an alias for belongs_to.
Greg
Greg F.
The Norcross Group
Forensics for the 21st Century
On Jan 31, 2006, at 10:22 AM, linus1412 wrote:
I have two models called entity and user.
The entities table has a column called users_id that contains the user
id of the user that created the entity.
In entity I have…
has_one :user
That’s your problem right there. If a user creates and entity, then
the entity belongs_to the user, and the user might create a lot of
entities, so the user has_many :entities. However, if you have the
restriction that a user can only create one entity, then you would
say that the user has_one entity.
Thus, your entity model should say…
belongs_to :user
-Derrick S.
“[The Perl source is] an interconnected mass
of livers and pancreas and lungs and little
sharp pointy things and the occasional
exploding kidney.” -Nat Torkington
linus1412 wrote:
I have two models called entity and user.
The entities table has a column called users_id that contains the user
id of the user that created the entity.
In entity I have…
has_one :user
… as I want to be able to show the user who created the entity from
the entity object.
But this produces the following error…
Mysql::Error: #42S22Unknown column ‘users.entity_id’ in ‘where clause’:
SELECT * FROM users WHERE (users.entity_id = 1) LIMIT 1
…Where I want it to select users by users.id.
What do I do? I bet it is easy.
Martin
The convention is that the model with the foreign key reference
‘belongs_to’ the model it points to.
So your entity table should have a ‘user_id’ column, and your ‘entity’
model ‘belongs_to :user’.
_Kevin
Plug: I wrote a small tutorial explaining has_many and belongs_to. Check
it out.