I have the following 2 models
class Sport < ActiveRecord::Base has_many :charts, order: "sortWeight ASC" has_one :product, :as => :productable accepts_nested_attributes_for :product, :allow_destroy => true end class Product < ActiveRecord::Base belongs_to :category belongs_to :productable, :polymorphic => true end
A sport can't exist without the product, so in my sports_controller.rb
I had:
def new @sport = Sport.new @sport.product = Product.new ... end
I tried to move the creation of the product to the sport model, using after_initialize
:
after_initialize :create_product def create_product self.product = Product.new end
I quickly learned that after_initialize
is called whenever a model is instantiated (i.e., from a find
call). So that wasn't the behavior I was looking for.
Whats the way I should be modeling the requirement that all sport
have a product
?
Thanks
Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.
What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.
Putting the logic in the controller could be the best answer as you stated, but you could get the after_initialize
to work by doing the following:
after_initialize :add_product def add_product self.product ||= Product.new end
That way, it only sets product if no product exists. It may not be worth the overhead and/or be less clear than having the logic in the controller.
Edit: Per Ryan's answer, performance-wise the following would likely be better:
after_initialize :add_product def add_product self.product ||= Product.new if self.new_record? end
Surely after_initialize :add_product, if: :new_record?
is the cleanest way here.
Keep the conditional out of the add_product function
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With