Given the following situation: client belongs_to entity entity has_one client entity has_one vendor entity has_many contacts entity has_many sites etc. Creating a new client should test if an entity with the existing name is already on file and return it if so, else the process should create a new entity with identically named attributes in entity passed the initial values from client. I think that this sort of complexity belongs in the client model but I am open to arguments as to where else it might go. Is there a recommended Rails approach that deals with this situation? Should I do all this creating and initializing in a before_create specified method? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
James Byrne wrote:> Given the following situation: > > client belongs_to entity > > entity has_one client > entity has_one vendor > entity has_many contacts > entity has_many sites > etc. >class SomeController < ApplicationController def find_or_create_client # Don''t use find :first as it will throw found = Entity.find(:all, :conditions => [''matching_field = ?'', params[:entity][:matching_field]]) if found.blank? # Please refer to rails casts on how to do creates through associations # I think it''s complex forms part 1, 2, and 3 @client = Entity.create(params[:entity]).client else @client = found.first.entity.client end end end hth ilan -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---