Lighting the lightbulb

Hi all,
this is my first post here. I have several Rails books here that I'm working and I'm trying to "get the light bulb to turn on."

I'm attempting a small application that processes account applications, and if approved, turns them into clients. I've got account_application and client models. I have the a_a side working reasonably well. I have a list of pending applications, I can review them individually, and approve or decline them.

Upon approval what I want to do is conceptually simple, but I just can't figure out where it "belongs." When I have an approved application I'd like to create a client based on that application. The client will get an an account number and such, and also retain a "has one" relationship with the application.

Where do I make that happen? In the client model? The client controller?
Also, can I create an account application with a "belongs to" before I have a client to which it can belong?

I think my biggest hurdle is that I don't yet grasp how the Rails "magic" works, and so I don't know how to fit it in.

Thanks for any guidance,

-George

Hi all,
this is my first post here. I have several Rails books here that I'm
working and I'm trying to "get the light bulb to turn on."

I'm attempting a small application that processes account
applications, and if approved, turns them into clients. I've got
account_application and client models. I have the a_a side working
reasonably well. I have a list of pending applications, I can review
them individually, and approve or decline them.

Upon approval what I want to do is conceptually simple, but I just
can't figure out where it "belongs." When I have an approved
application I'd like to create a client based on that application.
The client will get an an account number and such, and also retain a
"has one" relationship with the application.

Where do I make that happen? In the client model? The client controller?
Also, can I create an account application with a "belongs to" before
I have a client to which it can belong?

I think my biggest hurdle is that I don't yet grasp how the Rails
"magic" works, and so I don't know how to fit it in.

You could have an application without a client by setting client_id to nil. Can't think of a reason that wouldn't work.

But you could also create the client fully on submission and if the application is denied and it's the only one that client has, remove them. Or have some other status field for the client or something such that you can create it when the application is submitted.

-philip

You can definitely declare the relationship before the records exist. Just declare the relationships as you would if there were data.

As to creating the account, you may want to look into ActiveRecord callbacks [1] and have something like

class AccountApplication < ActiveRecord::Base
   after_save :process_new_account

   def process_new_account
     if self.status == 'approved' and self.client.nil?
       # Create client here
     end
   end
end

James.

1: http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks.html

Hey! The light bulb just got a little less dim! :slight_smile:

That feels right, and I’d never have thought of it.

Thanks!

-George