Question: I have a table of statuses, each of which have a name attribute. Currently I can do:

```
FooStatus.find_by_name("bar")

```

And that's fine. But I'm wondering if I could do:

```
FooStatus.bar

```

So I have this approach:

```
class FooStatus < ActiveRecord::Base
  def self.method_missing(meth, *args, &block)
    if self.allowed_statuses.include?(meth.to_s.titleize)
      self.where("name = ?", meth.to_s.titleize).first
    else
      super(meth, *args, &block)
    end
  end

  def self.allowed_statuses
    self.pluck(:name)
  end
end

```

The above code works, but it leads to the following weird behavior:

```
FooStatus.respond_to?(:bar) => false
FooStatus.bar => #<FooStatus name: 'bar'>

```

That's not great, but if I try to implement respond\_to?, I get a recursion problem

```
class FooStatus < ActiveRecord::Base
  def self.method_missing(meth, *args, &block)
    if self.allowed_statuses.include?(meth.to_s.titleize)
      self.where("name = ?", meth.to_s.titleize).first
    else
      super(meth, *args, &block)
    end
  end

  def self.allowed_statuses
    self.pluck(:name)
  end

  def self.respond_to?(meth, include_private = false)
    if self.allowed_statuses.include?(meth.to_s.titleize)
      true
    else
      super(meth)
    end
  end
end

```

And that gets me:

```
FooStatus.bar => ThreadError: deadlock; recursive locking

```

Any ideas on getting method\_missing and respond\_to to work together?

Answer: 
I don't know if I'd recommend your approach... seems too magical to me and I worry about what happens when you have a status with a name of 'destroy' or some other method you might legitimately want to call (or that Rails' calls internally that you aren't aware of).

But... instead of mucking with method missing, I think you'd be better off extending the class and automatically defining the methods by looping through allowed\_statuses and creating them. This will make respond\_to? work. And you could also check to make sure it's not already defined somewhere else...
Use a scope.

```
class FooStatus < ActiveRecord::Base
  scope :bar, where(:name => "bar")

  # etc
end

```

Now, you can do `FooStatus.bar` which will return an ActiveRelation object. If you expect this to return a single instance, you could do `FooStatus.bar.first` or if many `FooStatus.bar.all`, or you could put the `.first` or `.all` on the end of the scope in which case it'll return the same thing as the finder.

You can also define a scope with a lambda if the input isn't constant (not always "bar"). [Section 13.1 of this guide has an example](http://guides.rubyonrails.org/active_record_querying.html#scopes)