# Lighting the lightbulb

**URL:** <https://www.ruby-forum.com/t/lighting-the-lightbulb/103102>\
**Category:** Rails\
**Created:** [May 1, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/lighting-the-lightbulb/103102 "2007-05-01T22:15:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 1, 2007, 10:15pm UTC](https://www.ruby-forum.com/t/lighting-the-lightbulb/103102/1 "2007-05-01T22:15:51Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 1, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/lighting-the-lightbulb/103102/2 "2007-05-01T22:26:53Z")

</div>

> Upon approval what I want to do is conceptually simple, but I just  
> “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

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 1, 2007, 10:50pm UTC](https://www.ruby-forum.com/t/lighting-the-lightbulb/103102/3 "2007-05-01T22:50:56Z")

</div>

On May 1, 2007, at 4:15 PM, List Catcher wrote:

> 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 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](http://rails.rubyonrails.org/classes/ActiveRecord/Callbacks.html)

–  
James S.

> **[Processing Greenbelt 2009](https://jystewart.net/2009/09/08/processing-greenbelt-2009/)**
>
> We did, in fact, make a newspaper. The hour or so after my last post were more than a little stressful, but the results (a few typos and a printing hiccup that most certainly wasn’t our fault…

---

<div class="post-metadata">

**Author:** ![List\_Catcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/aca169/32.png) [@List\_Catcher](https://www.ruby-forum.com/u/List_Catcher)\
**Post date:** [May 1, 2007, 11:17pm UTC](https://www.ruby-forum.com/t/lighting-the-lightbulb/103102/4 "2007-05-01T23:17:00Z")

</div>

On May 1, 2007, at 2:49 PM, James S. wrote:

> end  
> end

Hey! The light bulb just got a little less dim! 🙂  
That feels right, and I’d never have thought of it.

Thanks!

-George
