Forum: Ruby on Rails Help creating and using database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
71e53b54803415f87ef1b898baf8f3ca?d=identicon&s=25 John Smith (terry_wolf)
on 2008-12-22 10:16
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?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-22 10:21
(Received via mailing list)
On Dec 22, 9:16 am, John Smith <rails-mailing-l...@andreas-s.net>
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
71e53b54803415f87ef1b898baf8f3ca?d=identicon&s=25 John Smith (terry_wolf)
on 2008-12-22 10:29
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?
7e35ecb8cb04c4c15cf0ad1a0edf113d?d=identicon&s=25 anton effendi (Guest)
on 2008-12-22 11:44
(Received via mailing list)
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



On Mon, Dec 22, 2008 at 4:29 PM, John Smith <
71e53b54803415f87ef1b898baf8f3ca?d=identicon&s=25 John Smith (terry_wolf)
on 2008-12-22 11:56
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?
53be54e5db4dc58e4980db5a8255621b?d=identicon&s=25 Harold (Guest)
on 2008-12-22 17:09
(Received via mailing list)
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 Bates:
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 Smith <rails-mailing-l...@andreas-s.net>
71e53b54803415f87ef1b898baf8f3ca?d=identicon&s=25 John Smith (terry_wolf)
on 2008-12-23 08:57
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 Bates:
> 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 Smith <rails-mailing-l...@andreas-s.net>

Thanks a lot. I will check it.
80e4cb97cae5c8d745f72337d93fd8f2?d=identicon&s=25 MaD (Guest)
on 2008-12-23 11:42
(Received via mailing list)
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.
This topic is locked and can not be replied to.