I have two migrations as below:
CreateGroups
t.string :name
t.string :groupkey
CreateEvents
t.string :name
t.string :groupkeyname
t.integer :group_id
and their corresponding models:
group
has_many :events
event
belongs_to :group
from what I understand is that the t.integer :group_id column in the CreateEvents migration references the default id column in the CreateGroups migration. Now I create a group:
g = Group.create(:name => "skydive", :groupkey => "withwingsuit")
However, when I create an event like this:
Event.create(:name => "whatever", :group_id => 1)
and try to print g.events there is nothing it gives me: []
Why doesn’t it give the event i just created ?
you may need to refresh the association because it may be cached locally. After adding the new event try this.
that will force Rails to retrieve items again from the DB.