Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails after_initialize only on "new"

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

like image 206
Tyler DeWitt Avatar asked Mar 07 '12 22:03

Tyler DeWitt


People also ask

How do Rails Callbacks work?

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 Active Record in Ruby on Rails?

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.


2 Answers

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 
like image 112
bostonou Avatar answered Sep 23 '22 08:09

bostonou


Surely after_initialize :add_product, if: :new_record? is the cleanest way here.

Keep the conditional out of the add_product function

like image 35
Paul Odeon Avatar answered Sep 21 '22 08:09

Paul Odeon