Page 266 - Asterisk™: The Future of Telephony
P. 266

class Customer < ActiveRecord::Base
                     belongs_to :group
                     validates_presence_of   :name, :phone_number
                     validates_uniqueness_of :phone_number
                     validates_associated    :group
                     def total_bill
                   self.group.hourly_rate * self.usage_this_month / 1.hour
                     end
                   end
                   class Group < ActiveRecord::Base
                     has_many :customers
                     validates_presence_of :description, :hourly_rate
                   end
               From just this small amount of information, ActiveRecord can make a lot of logical
               inferences. When these classes interpret, ActiveRecord assumes the table names to be
               customers and groups respectively by lowercasing the classes’ names and making them
               plural. If this convention is not desired, the author can easily override it. Additionally,
               at interpretation time, ActiveRecord actually peeks into the database’s columns and
               makes available many new dynamically created methods.
               The belongs_to and has_many methods in this example define relationships between
               Customers and Groups. Notice again how ActiveRecord uses pluralization to make the
               code more expressive in the has_many :customers line. From this example, we also see
               several  validations—policies  that  ActiveRecord  will  enforce.  When  creating  a  new
               Customer we must provide a name and phone_number at the bare minimum. No two phone
               numbers can conflict. Every Customer must have a Group. Every Group must have a
               description and hourly_rate. These help both the developer and the database stay on
               track.
               Also, notice the total_bill method in the Customer class. On any Customer object we
               extract from the database, we can call this method, which multiplies the hourly_rate
               value of the group to which the Customer belongs by the Customer’s own phone usage
               this month (in seconds).
               Here are a few examples that may clarify the usefulness of having Ruby objects abstract
               database logic:
                   everyone = Customer.find :all
                   jay = Customer.find_by_name "Jay Phillips"
                   jay.phone_number # Performs a SELECT statement
                   jay.total_bill   # Performs arithmetic on several SELECT statements
                   jay.group.customers.average :usage_this_month
                   jay.group.destroy
                   jay.group = Group.create :description => "New cool group!",
                                            :hourly_rate => 1.23
                   jay.save


               238 | Chapter 10: Asterisk Manager Interface (AMI) and Adhearsion
   261   262   263   264   265   266   267   268   269   270   271