What is a Model?
A Model Is A Class
A "Model" is a Class that deals with the manipulation of data. Classes allow the combination of data and methods in one place. Because it is a full-blown Class, it can have any number of its own variables and methods.
The Model class is the perfect place for data validation for a Rails system. It actually makes more sense to store data-based logic here than in the database (assuming that other systems are not monkeying with your data). This is one of the reasons most Rails programmers don't use database constraints (one of the big reasons is ActiveRecord doesn't like it either).
Generally, a Model Class is comprised of several things:
- Associations (Relationships to other tables in the database)
- Validations (special rails "validates..." statements)
- Accessors (creates "getter" and "setter" methods automatically) (methods used to get/put data in a private class variable)
- CallBacks (hooks into the underlying rails logic that allow you to inject your code a specific steps)
- Class Variables (@@varname, only one set maintained across all objects of the class)
- Instance Variables (@varname, each object has its own set to manipulate at will)
- Class Methods (generic logic methods not tied to specific data) (or operate on the class itself, like instance counters)
- Instance Methods (any method that is not a class method)
# Associations ================================================ has_one :order_account_type has_many :orders belongs_to :order_address belongs_to :order_user belongs_to :order
# Validations ======================================================= validates_presence_of :order_user_id, :order_address_id, :order_id validates_length_of :cc_number, :maximum => 20 validates_format_of :cc_number, :with => /^[\d]*$/, :message => ERROR_NUMBER validates_format_of :credit_ccv, :with => /^[\d]*$/, :message => ERROR_NUMBER validates_numericality_of :expiration_month, :expiration_year
# Accessors ======================================================= attr_accessor :name, :promotion_code, cc_number
# CALLBACKS ========================================================== before_save :set_product_cost before_destroy{|record | Person.destroy_all "firm_id=#{record.id}"}
During the life cycle of an active record object, you can hook into 8 events: (-) save (-) valid? (1) before_validation (2) before_validation_on_create (-) validate (-) validate_on_create (3) after_validation (4) after_validation_on_create (5) before_save (6) before_create (-) create (7) after_create (8) after_save # CLASS VARIABLES ====================================================== @@number_of_instances @@version
# INSTANCE VARIABLES ======================================================= @total_line_items @time_created
# CLASS METHODS ======================================================= # List of years for dropdown in UI def self.years start_year = Date.today.year years = Array.new 10.times do years << start_year start_year += 1 end return years end
# Search by order_number def self.search(search_term, count=false, limit_sql=nil) if (count == true) then sql = "SELECT COUNT(*) " else sql = "SELECT DISTINCT orders.* " end sql << "FROM orders " sql << "WHERE orders.order_number = ? " sql << "ORDER BY orders.created_on DESC " sql << "LIMIT #{limit_sql}" if limit_sql arg_arr = [sql, search_term, "%#{search_term}%"]
if (count == true) then count_by_sql(arg_arr) else find_by_sql(arg_arr) end end
# INSTANCE METHODS ========================================================== def status code = OrderStatusCode.find(:first, :conditions => ["id = ?", self.order_status_code_id]) code.name end
def tax_cost (self.line_items_total) * (self.tax/100) end
def name return "#{billing_address.first_name} # billing_address.last_name}" end
POSTED BY Just A Guy AT 12/31/2007 1:50 PM
0 COMMENTS
» POST A COMMENT
| DIGG IT
« BACK HOME
|
 |
MY PROFILE
Name: Just A Guy
Location: Atlanta, GA
I've been programming for over 20 years (started when I was 2). When Microsoft decided to end new releases of my favorite development tool, Visual FoxPro (VFP), I had to decide if I wanted to keep drinking the Redmond Kool-Aid or step off that treadmill. I'm a consultant, so I must be very productive to survive. I found Ruby and Rails. Most of what I post here will be things I had to learn the hard way and how I am making the transition from a Windows-based (mainly) desktop tool to open-source web-based tools.
Also see http://oomoo.wordpress.com
(begin...rescue is part of the Ruby exception handling)
RECENT POSTS
Public Models

|