Reputation: 869
I have the following:
class Publication < ActiveRecord::Base
belongs_to :project, :inverse_of => :publication
before_create :bind_project
private
def bind_project
self.create_project
end
end
class Project < ActiveRecord::Base
has_one :publication, :inverse_of => :project
end
According to this when creating a new Publication
the publication_id
attribute on the project
model should be set by the create_project
method.
Why it does not happen?
This is what I see on bind_project
:
self.project_id
is set correctlyself.project.publication_id
is NULL
self.project.publication.id
is set correctlyThe database reflects this also: the projects.publication_id
column is NULL
.
Upvotes: 0
Views: 903
Reputation: 741
Seems a bit strange, that you try to access the create_project
method in a before_create
callback. Typo? An after_create
callback seems to be more appropriate.
Moreover: What for do you need the publication_id
attribute on the has_one
side of an association? There only needs to be one _id
attribute on the belongs_to
side.
Addendum to my first paragraph: As I see it, you are trying to use Rails magic on the associated object (the create_project
method) before the actual base object is finished being created. Although this might work, this would be my first point to investigate.
Upvotes: 1