Coder Social home page Coder Social logo

alexhayes / activerecord_symbolize Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zargony/activerecord_symbolize

1.0 2.0 0.0 105 KB

Symbolize attribute values in ActiveRecord (e.g. for nicer enums)

Home Page: http://zargony.com/

License: MIT License

Ruby 100.00%

activerecord_symbolize's Introduction

= Symbolize attribute values in ActiveRecord (e.g. for nicer enums)

This plugin introduces an easy way to use symbols for values of ActiveRecord
attributes. Symbolized attributes return a ruby symbol (or nil) as their value
and can be set using symbols.

== About

Since ActiveRecord does not natively support database column types of ENUM or
SET, you'll usually use a string attribute and restrict it to certain values
with validations. Using this plugin, the values of such pseudo-enums are
symbols, which look more ruby-style than strings.

Simply add "symbolize :attr_name" to your model class, and the specified
attribute will return symbol values and can be set using smbols (setting
string values will still work, which is important when using forms).

An attribute to symbolize should be a string (varchar) column in the database.

Blog: http://zargony.com/
Github: http://github.com/zargony/activerecord_symbolize

== Install

  ./script/plugin install git://github.com/zargony/activerecord_symbolize.git

== Usage

Add "symbolize :attr_name" to your model class. An attribute validation
(like validates_inclusion_of) can be added by using the :in option.

  class User < ActiveRecord::Base
    symbolize :gender, :in => [:female, :male]
  end

== Examples

  u = User.find_by_name('Anna')   # => #<User Anna>
  u.gender                        # => :female
  
  u = User.find_by_gender(:male)  # => #<User Bob>
  u.gender                        # => :male
  
  u = User.find(:all, :conditions => { :gender => :female })
  
  u = User.new(:name => 'ET', :gender => :unknown)
  u.save                          # => validation fails

== Notes

I've been using this for quite some time and made it a rails plugin now. More
background information can be found at
http://zargony.com/2007/09/07/symbolize-attribute-values-in-activerecord


Copyright (c) 2007-2008 Andreas Neuhaus, released under the MIT license

activerecord_symbolize's People

Contributors

alexhayes avatar zargony avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.