# Help creating and using database

**URL:** <https://www.ruby-forum.com/t/help-creating-and-using-database/156183>\
**Category:** Rails\
**Created:** [December 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183 "2008-12-22T10:16:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 10:16am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/1 "2008-12-22T10:16:12Z")

</div>

I have this problem.  
I have a Job table in my db, and each job has many steps (table Step).  
Is not possible to know how many steps a job has until it is created. So  
when I create a new job I have a problem: I can not have a step\_id in my  
Job table, because it can be one step or more.  
What can I do?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 10:21am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/2 "2008-12-22T10:21:33Z")

</div>

On Dec 22, 9:16 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I have this problem.  
> I have a Job table in my db, and each job has many steps (table Step).  
> Is not possible to know how many steps a job has until it is created. So  
> when I create a new job I have a problem: I can not have a step\_id in my  
> Job table, because it can be one step or more.  
> What can I do?

If jobs has\_many :steps then the jobs table doesn’t have a step\_id at  
all: steps have a job\_id column.

Fred

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 10:29am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/3 "2008-12-22T10:29:07Z")

</div>

Thanks, I know.  
If I can create a job first I have no problem, but I can’t do that. I  
want to be able to create a job and, at the same time, create as many  
steps as I want. I would use AJAX in order to create new step fields,  
but if I have not created a job I can have any relation between a step  
and a job.  
Any ideas?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 11:44am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/4 "2008-12-22T11:44:46Z")

</div>

HAii… sorry my English is very bad  
I don’t what you want… but I have idea  
why do u not use 3 model / 3 table?

table job  
id

table join\_job\_step  
job\_id  
step\_id

table step  
id

this is “has\_many\_and\_belongs\_to” (same), but Don’t use  
“has\_many\_and\_belongs\_to”.  
Model:

- job  
has\_many :join\_job\_steps

- join\_job\_step  
belongs\_to :job

- step  
has\_may :join\_job\_steps

So, when u create job or step, u don’t need step\_id or job\_id… And if u  
want make relation, just create join\_job\_step.

May be it can help u…  
Thank you

–  
Wu You Duan(Anton effendi)  
Senior Rails Developer  
[www.kiranatama.com](http://www.kiranatama.com)

On Mon, Dec 22, 2008 at 4:29 PM, John S. \<

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 5:09pm UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/5 "2008-12-22T17:09:50Z")

</div>

I think what you’re looking for is the build method, something along  
the lines of @job.steps.build in your controller.

Take a look at the complex form railscasts by Ryan B.:

> **[\#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)**
>
> Short Ruby on Rails screencasts containing tips, tricks and tutorials. Great for both novice and experienced web developers.

(go to parts 2 and 3 after you’re done with part 1).

On Dec 22, 5:56 am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 22, 2008, 11:56am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/6 "2008-12-22T11:56:12Z")

</div>

Thanks a lot, but I think that does not solve my problem, because I need  
to create a job to make a relation between the job and it steps. With  
that, I can create steps without a job, but how can I make relation  
between these steps and the job. Should I have to take the last steps  
and make a relation with the job I am creating? I think this is a bas  
solution.  
Any other ideas?

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 23, 2008, 11:42am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/7 "2008-12-23T11:42:50Z")

</div>

sorry to say, but i think what you really need is a guide on data-  
modeling.

if a job has many steps, the job has to be there before you create any  
steps, as every step belongs to exactly one job.

now, you say you have to create a job and its steps at the same time.  
why don’t you think of a way to solve that? look into your controller,  
there is always a solution.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [December 23, 2008, 8:57am UTC](https://www.ruby-forum.com/t/help-creating-and-using-database/156183/8 "2008-12-23T08:57:57Z")

</div>

Harold wrote:

> I think what you’re looking for is the build method, something along  
> the lines of @job.steps.build in your controller.
> 
> Take a look at the complex form railscasts by Ryan B.:  
> [#73 Complex Forms Part 1 - RailsCasts](http://railscasts.com/episodes/73-complex-forms-part-1)  
> (go to parts 2 and 3 after you’re done with part 1).
> 
> On Dec 22, 5:56ï¿½am, John S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

Thanks a lot. I will check it.
