How to use a lookup table?

Hi,

I have a table in database which has description of all the statuses
that an account can have and their flag values, like 0 means unapproved,
1 meanns approved, 3 means suspended and so on. Now, when I am inserting
an account record or updating then how do I get the value from this
status table if I want flag value for apprroved or suspended etc.?
Basically, how do I know that I have to set flag to 0, 1 or 2 for
approved?

thanks.

From what I can gather it sounds like you have a user table that might
look like this. Of course it could have a lot more columns as
well…but these are the ones you seem to be dealing with.


|user_id | approved | unapproved | suspended |
|-----------------------------------------------------------------|
| 23 | 1 | 0 | 0 |
|_____________________________________|

why do you need a 2nd table?

Just read the property directly from the users table. You’d be
duplicating data if you add another table and slowing down mysql by
needing to join.

Maybe I’m wrong or misunderstood, but that seems like it would work to
me.
-Jon

User.create({ :name => ‘joe’, :status =>
Status.find_by_name(‘Approved’).value })?

It might be better if you removed the ‘value’ column from the Status
model and then use a has_one relationship from User to Status:

statuses(id, status_name)

class Status < ActiveRecord::Base
end

users(id, name, status_id)

class User < ActiveRecord::Base
has_one :status
end

User.create({:name => ‘joe’, :status => Status.find_by_name(‘Approved’) })
User.find(:all, :include => :status).map(&:status_name)

What I have is a user table like :

id
name
status

this status could be 0 for pending, 1 for approved, 2 for hold etc.

And there is another table as statuses:
id
value
desc

with row data as :

1,0,Pending
2,1,Approved
3,2,Hold

Now how do I use this status table if I want to create an user record
with approved status?

kinneyjd wrote:

From what I can gather it sounds like you have a user table that might
look like this. Of course it could have a lot more columns as
well…but these are the ones you seem to be dealing with.


|user_id | approved | unapproved | suspended |
|-----------------------------------------------------------------|
| 23 | 1 | 0 | 0 |
|_____________________________________|

why do you need a 2nd table?

Just read the property directly from the users table. You’d be
duplicating data if you add another table and slowing down mysql by
needing to join.

Maybe I’m wrong or misunderstood, but that seems like it would work to
me.
-Jon

Eden Li wrote:

statuses(id, status_name)

class Status < ActiveRecord::Base
end

users(id, name, status_id)

class User < ActiveRecord::Base
has_one :status
end

User.create({:name => ‘joe’, :status => Status.find_by_name(‘Approved’) })
User.find(:all, :include => :status).map(&:status_name)

It’s actually belongs_to, which goes where the FK is.

Status has_many Users, User belongs_to Status.

A.