Hi,<br><br>I use a gem file.<br><br>In your gemfile add:<br><br>gem 'enumerated_attribute'<br><br>In your model:<br><br>enum_attr :doc_type, %w(diploma teacher_reference student_reference course_outline)<br><br>Otherwise you can use (in your model) something like this:<br>
<br>  ROLES = %w[admin manager user].freeze<br>  <br>  def role_in_list<br>    errors.add(:base, "Role #{self.role} is not allowed") unless User::ROLES.include?(self.role)<br>  end<br><br><div class="gmail_quote">
On 7 September 2011 10:46, Chris Kimpton <span dir="ltr"><<a href="mailto:chris@kimptoc.net">chris@kimptoc.net</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hi,<br>
<br>
Could I pick your brains a little...<br>
<br>
I have some status values that I want to store in the DB, but also be<br>
available in an enum-like way (perhaps that is my mistake...)<br>
<br>
My current code is like this:<br>
<br>
class Category < ActiveRecord::Base<br>
<br>
  has_many :widgets<br>
<br>
  def self.CAT1<br>
    find_by_cat_name("cat1")<br>
  end<br>
<br>
end<br>
<br>
This seems to work fine on its own, and when used via the console.<br>
<br>
I'd like to use it in a "scope" to restrict a widget query, like so:<br>
<br>
<br>
class Widget < ActiveRecord::Base<br>
<br>
  belongs_to :category<br>
<br>
  scope :one, where(:category_id => Category.CAT1).order(:created_at)<br>
<br>
end<br>
<br>
Again, this works fine in the console, I can add widgets to the category<br>
and the scope picks up the new values.<br>
<br>
But when I try the same via my tests, it is not finding the newly created<br>
widgets.  From the logs, it seems the Category id is not set - as if the<br>
scope is using an uninitialised version of the Category object.<br>
<br>
  test "scope" do<br>
    assert_equal 0, Widget.all.count<br>
    assert_equal 0, Widget.one.count<br>
    assert_equal 0, Widget.two.count<br>
    assert_not_nil Category.CAT1<br>
    assert_not_nil Category.CAT2<br>
    Widget.create(:name => "foo", :size => 2, :category => Category.CAT2)<br>
    assert_equal 1, Widget.all.count<br>
    assert_equal 0, Widget.one.count<br>
    assert_equal 1, Widget.two.count  # this assert fails - gets a 0.<br>
<br>
  end<br>
<br>
Perhaps I am doing something wrong...<br>
<br>
The full (test) project is here -<br>
<a href="https://github.com/kimptoc/scope-enum-tester" target="_blank">https://github.com/kimptoc/scope-enum-tester</a> - using Rails 3.1 (not tried<br>
3.0 to see if that could be the issue...)<br>
<br>
Thanks,<br>
Chris<br>
<br>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div><br><br clear="all"><br>-- <br>Riccardo Tacconi<br>Ruby on Rails and PHP development - System Administration<br>VIRTUELOGIC LIMITED<br><br><a href="http://github.com/rtacconi" target="_blank">http://github.com/rtacconi</a><br>
<a href="http://riccardotacconi.blogspot.com" target="_blank">http://riccardotacconi.blogspot.com</a><br><a href="http://twitter.com/rtacconi" target="_blank">http://twitter.com/rtacconi</a><br><a href="http://www.linkedin.com/in/riccardotacconi" target="_blank">http://www.linkedin.com/in/riccardotacconi</a><br>