Boolean database + Site Design/Architecture Question

Hi all,

As usual, you've all been very helpful in the past and I thank you for
continuing to help myself and others.

Today I have the following code/database architecture question which I
would like you guys to help me out with.

Children Table (user has_many children)

I would go with a single 'status' field.

Roy Pardee wrote:

I would go with a single 'status' field.

Thanks Roy, I appreciate the reply. I guess I could just do a...

if @child.status == 'lost'

I did like the elegance of boolean for things like:

if @child.is_lost?

Any other suggestions or opinions?

Well, you can still have those is_whatever? methods--just write them on the model your own self:

class Child
  def is_lost?
    self.status == 'lost'
  def is_found?
    self.status == 'found'
  def is_safe?
    self.status = 'safe'

You could even write your own setters (def is_safe=(new_val)), tho that may be taking it too far...

Roy Pardee wrote:

Well, you can still have those is_whatever? methods--just write them on
the model your own self:

Touché. Can't believe I didn't think about that! Thank you!!

First and foremost, shouldn't your model be called Child? A single child
shouldn't be called "a children", but that's how you have it set up. Having
it as is won't destroy the universe, but it will end up getting really
confusing sooner or later. Rails knows that "children" is the plural form
of "child". Open the rails console and type "child".pluralize to see for

That aside, since Children is a stateful object, you should consider the Acts
As State Machine plugin. All you need is the plugin and a "state" (varchar)
column in the model's database table. This gives you all kinds of neat

In your rails app root, do this little number:
script/plugin install

We will configure your model to have three states: "safe", "lost",
and "found". In your Children model, add the following:

#set the default state
acts_as_state_machine, :initial => :safe

# declare three states: "safe", "lost", and "found"
state :safe
state :lost
state :found

# configure state methods
event :lost do
  transitions :from => :safe, :to => :lost
  transitions :from => :found, :to => :lost
event :found do
  transitions :from => :safe, :to => :found
  transitions :from => :lost, :to => :found
event :safe do
  transitions :from => :lost, :to => :safe
  transitions :from => :found, :to => :safe

That's it. Now the following methods will work to check the state: # true/false
@child.lost? # true/false
@child.found? # true/false

The "events" that you declared are used to change the object's state, using
event!. For example, to change a safe child to lost, you would call:
Normally, you would want to name your "events" some kind of verb. This makes
the method easier to read and understand, but your situation is peculiar. I
guess you should leave them as is to avoid any further confusion.

You can tell AASM to call additional methods when transitioning between
states, if you wish. You can also block transitions unless certain conditions
are met. Read up on the callbacks and guard functions here:
(site is currently showing a blank page, but it's there, I swear!)

I hate to bump and annoy, but can anyone offer a suggestion on my last