Reputation: 3099
I have some problems understanding the has_one
relation.
I have two models Planning
and Sport
. A Planning
is concerned by one Sport
and a Sport
can be found in many Planning
s. So I defined my Planning
this way:
class Planning
...
has_one :sport
...
end
And I didn't add anything in Sport
regarding the Planning
.
In my mind, this would allow me to access planning.sport
but I don't mind if I can't find sport.plannings
(which makes no sense).
The problem is that when I try to create a new Planning
using my ActiveAdmin
interface, I have the following error as soon as I try to access the plannings/new
page:
undefined method `planning_id' for #<Planning:0x30d8570>
What I understand is that it checks in Sport
that a planning_id
exists, but my understanding was that there was just a sport_id
in the Planning
, not the opposite...
What did I do wrong? Do I need to use an intermediary model? Is this jsut linked to ActiveAdmin
?
Thanks!
Upvotes: 0
Views: 69
Reputation: 29349
What you need is
class Planning
...
belongs_to :sport
...
end
class Sport
has_many: plannings
end
You can think of it like, whoever has the foreign key is like the child. So here planning has the foreign key of sport. So it is a child of Sport and it belongs to sport.
Upvotes: 3