Forum: Ruby on Rails Ruby on Rails DB Relation

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.
60289ef6f381703a6bf5991a00bcedca?d=identicon&s=25 Shabbir Ahmed (shabbir)
on 2016-01-11 05:30
For suppose I have three database table eg:
recruiters
------------------------------------
recruiter_id | name | email       |
------------------------------------
1            | xyz  | x@gmail.com |
------------------------------------

companies
------------------------------------
company_id | recruiter_id | c_name |
------------------------------------
1          | 1            | abc    |
------------------------------------

posts
------------------------------------
post_id | company_id | post_title |
------------------------------------
1       | 1          | etc        |
------------------------------------

This is my database.

Now I need at first I signup then create 1 or 2 or 3 companies into one
recruiter_id then create post into company_id.

Database already created but How can I relation this model.

Thanks
4c6bde00168d595053c09aac7e487f8e?d=identicon&s=25 Colin Law (Guest)
on 2016-01-11 09:19
(Received via mailing list)
On 11 January 2016 at 04:30, Shabbir Ahmed <lists@ruby-forum.com> wrote:
> company_id | recruiter_id | c_name |
>
> This is my database.
>
> Now I need at first I signup then create 1 or 2 or 3 companies into one
> recruiter_id then create post into company_id.
>
> Database already created but How can I relation this model.


I assume that you are a newcomer to rails.  You have not used the
rails convention for naming the primary id field in each table, which
is a bad idea, it is better to stick to the conventions unless there
is very good reason.  I suggest before going further that you work
right through a good tutorial such as railstutorial.org (which is free
to use online).  That will show you the basics of rails.  You will
recover the time spent on this very quickly.

However, back on the specific problem, you are looking at the problem
from the wrong end.  You should not say "this are my tables, what
should the associations be", but rather "this is my problem, what are
the associations" from which you can design the tables.  It looks from
your description of the problem that you want

Recruiter has_many companies

Company belongs_to recruiter
Company has_many posts

Post belongs_to company

But as I said, please work through the tutorial before going further.
Also look at the Rails Guides.

Colin
This topic is locked and can not be replied to.