I have a basic rails question where I need to save two associated objects.
The association is Rtake has_many :companies and Company belongs_to :rtake
def create
@rtake = RTake.new(:email => params[:contact_email])
@rtake.role = "PROVIDER"
@company = @rtake.companies.build(params[:company])
@company.rtake = @rtake
respond_to do |format|
if @company.save_company_and_rtake
format.html{ redirect_to admin_companies_url}
else
flash.now[:errors] = @company.errors.full_messages.join(", ")
format.html{ render "new" }
end
end
end
In my company.rb class I have
def save_company_and_rtake
status1 = self.save(:validate => false)
status2 = self.rtake.save(:validate => false)
status = status1 && status2
status
end
The problem I face is that the company.rtake_id remains nil. Ideally shouldn’t the company.rtake_id get updated to the @rtake.id after save.
I know I am missing something basic. Would appreciate some help.
You shouldn’t need this line:
@invitation is nil from what you’ve shown .
But also, when you built the
@company,@rtake.idisn’t set because it hasn’t been saved.