# User and status table definition and relation

**URL:** <https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086>\
**Category:** Rails\
**Created:** [January 5, 2010, 7:51am UTC](https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086 "2010-01-05T07:51:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 5, 2010, 7:51am UTC](https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086/1 "2010-01-05T07:51:54Z")

</div>

Hi

```
I have models user and user_status An user always elongs to a

```

particular status at any time Status table has predefined values like  
“pending”,“approved” etc My question is what should be the table  
structure and relation between these models? I am trying like putting  
status\_id in users table And the relation is

user belongs\_to user\_status

If so user\_status has\_one user Is this correct?Please guide me

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 5, 2010, 10:39am UTC](https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086/2 "2010-01-05T10:39:50Z")

</div>

On Jan 5, 6:51 am, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi
> 
> ```
> I have models user and user_status An user always elongs to a
> 
> ```
> 
> particular status at any time Status table has predefined values like  
> “pending”,“approved” etc My question is what should be the table  
> structure and relation between these models? I am trying like putting  
> status\_id in users table And the relation is
> 
> user belongs\_to user\_status

If so then your table should be called user\_statuses and the column  
should be user\_status\_id.

> If so user\_status has\_one user Is this correct?Please guide me

No - since for a given status there are potentially many users in that  
status (so it’s a has\_many)

Fred

> ## Tom
> 
> Posted viahttp://www.ruby-forum.com/.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 5, 2010, 10:50am UTC](https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086/3 "2010-01-05T10:50:22Z")

</div>

2.7 Choosing Between belongs\_to and has\_one  
If you want to set up a 1–1 relationship between two models, you’ll  
need to add belongs\_to to one, and has\_one to the other. How do you  
know which is which?

The distinction is in where you place the foreign key (it goes on the  
table for the class declaring the belongs\_to association), but you  
should give some thought to the actual meaning of the data as well.  
The has\_one relationship says that one of something is yours – that  
is, that something points back to you. For example, it makes more  
sense to say that a supplier owns an account than that an account owns  
a supplier. This suggests that the correct relationships are like  
this:

class Supplier \< ActiveRecord::Base  
has\_one :account  
end

class Account \< ActiveRecord::Base  
belongs\_to :supplier  
end  
The corresponding migration might look like this:

class CreateSuppliers \< ActiveRecord::Migration  
def self.up  
create\_table :suppliers do |t|  
t.string :name  
t.timestamps  
end

```
create_table :accounts do |t|
  t.integer :supplier_id
  t.string :account_number
  t.timestamps
end

```

end

def self.down  
drop\_table :accounts  
drop\_table :suppliers  
end  
end

I just pasted from The wonderful rails guides:http://

> **[Active Record Associations — Ruby on Rails Guides](https://guides.rubyonrails.org/association_basics.html)**
>
> Active Record AssociationsThis guide covers the association features of Active Record.After reading this guide, you will know: How to declare associations between Active Record models. How to understand the various types of Active Record...

hope it helps,

Lake

On Jan 5, 1:51 am, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> If so user\_status has\_one user Is this correct?Please guide me
> 
> ## Tom
> 
> Posted viahttp://www.ruby-forum.com/.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [January 5, 2010, 10:50am UTC](https://www.ruby-forum.com/t/user-and-status-table-definition-and-relation/181086/4 "2010-01-05T10:50:55Z")

</div>

Hi Frederick

```
Thanks for your reply. So what i understood is, tables are users and 

```

user\_statuses .And users table has a column user\_status\_id Also the  
relation is

user belongs\_to user\_status  
user\_status has\_many users

```
    Is this right?

```

Tom
