I have the following:
app/models/order.rb
class Order < ActiveRecord::Base
belongs_to :client
accepts_nested_attributes_for :client
end
app/models/client.rb
class Client < ActiveRecord::Base
has_many :orders
end
When an Order is being saved I’d like to
- Check if there an existing Client with an email specified
- If exists, choose it for the Order
- If not, create a Client with the provided attribute and link it to my Order
Should I use before_save for the Client or what’s the best way of achieving it?
Thanks fellows!
Thanks to Fishz I played around and found the following solution:
Thanks =)