now available

now available
Want fresh tech tips in your inbox?

Trouble Using Attr_Accessor in Rails Models and Forms


You might use the attr_accessible method to create getters and setters for a class that has attributes which don't map directly to corresponding fields in a database. For example let's take the scenario where you are processing a credit card transaction. You don't want to save the credit card details, such as card number and verification value etc, however you still want to use these attributes in a form and you want to perform validations on them.

class CreditCardPurchase < ActiveRecord::Base
  attr_accessor :number, :cvv

The only probem that I've run into is using the attr_accessor on datetime select form fields. Rails won't be able to determine the "klass" and will spit out a nasty error. Looks there is some discussion around this bug. I've got another post about this topic http://seanbehan.com/ruby-on-rails/problem-slash-bug-in-rails-with-attr_accessor-and-datetime-select-fields/ but unfortunately not a whole lot of resultion :(

Tagged w/ #attr_accessor #forms #getters #rails #setters #troubleshooting #validations #virtual attributesruby on rails

Apps I've Built

XPath Expression Editor
Practice and improve your XPath skills with XPath Editor
Click to buy on the App Store
Photo Location Changer
Easily change the location on your photos and videos
Click here for more info about the app
Photo Date Changer
Easily change the dates and times on your photos and videos
Click here for more info about the app
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app