lamar lamar -4 years ago 70
Ruby Question

Rails how create association in the model before or after create?

i'm using a callback to create a association but after the creation did not associate

Someone have any hint how associate in the model instead of in the controller?

class Open < ActiveRecord::Base
after_create :images_build

def images_build
images.build

true
end
end

Answer Source

If you're trying to create an association and save it to the database then do:

before_create ->{ images.build }

This will create a single image record associated with your new Open record.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download