Relationship expects Destination Field
I'm sure this is so basic but I keep running into this when adding relationships. I haven't tried anything very complicated.. just adding simple has_one relationships.
I keep getting:
Here is my staff relationships:
And my Person attributes:
No person relationships ATM.
What am I missing?
3 Replies
So all relationships (many to many is a bit more involved) have a
source_attribute
and a destination_attribute
We assume default values for those
For belongs_to :foo, ...
we assume the source_attribute
is :foo_id
and the destination_attribute is :id
.
For has_one
we assume that the destination_attribute
is the last part of the source module name, snake cased, followed by id. So the resource you're showing must be called something like FlowerShopApp.Accounts.Staff
, and so we assume staff_id
. And for has_one
we assume the source_attribute
is :id
.
The way its currently set up, it has no way of knowing which staff
record belongs to a person
record.
Do you perhaps want a belongs_to :person, FlowerShopApp.Accounts.Person
? For belongs_to
relationships, we both assume that the source_attribute
is :person_id
and we define it automatically by default.Thanks. I have switched to belongs_to for these which works for me. I think I'm still not quite sure how has_one and belongs_to differ. But I'm thinking that I need to look at expanding my database and SQL knowledge. Thanks for pointing me in the right direction.
👍 generally speaking “belongs to” means “I have a field that points to you” and “has one” means “you have a field that points to me. If you were to change who a record “belongs to” you’d change a field on this record
So that’s why we define the attribute when using
belongs_to