To create new instances of Person class: foo = new Person({ id: 1, name: 'foo' }) bar = new Person({ id: 2, name: 'bar' })
Attribute values can be accessed using getter and setter functions: foo.get('name') foo.set('name', 'railsbob')
The Base class provides a collection array to store instances and they can be added as: Person.add(foo) Person.add(bar)
The collection belongs to the page context when used in a browser. Records …
def new_project(digest, project, person) @ digest, @ project, @ person = digest, project, person
attachments['digest.pdf'] = digest.to_pdf attachments['logo.jpg'] = File.read(project.logo_path)
mail( :subject => "Your digest for #{ project.name}", :to => person.email_address_with_name ) do |format| format.text { render :text => "Something texty" } format.html { render :text => "Something <i>texty</i>" …
…these structs you can output their gender or name in views without doing anything special: % dl [ @ person ] % dt = Person .human_attribute_name( :name ) % dd = @ person .name % dt = Person .human_attribute_name( :gender ) % dd = @ person .gender
In this example, Person is actually an aggregate of the entity Person and two value objects, called Name and Gender . Although all attributes are flattened out in your persistence layer (in this case, your database table); they …
You can use attribute names followed by question mark. Let's assume there is an ActiveRecord class Person , having first_name and last_name as attributes. p = Person.new p.first_name = 'Mike'
p.first_name? # => true p.last_name? # => false
Tip reblogged from milandobrota .
We have two models who's names aren't important so excuse me if I use the name Person and Address to represent them. They are nothing of the sort. In their purest form to replicate this issue, they are defined like this: class Address < ActiveRecord::Base has_and_belongs_to_many :people end
class Person < ActiveRecord::Base has_and_belongs_to_many :addresses accepts_nested_attributes_for :addresses
end
When we go to create a new Person record: Person.create(:addresses_attributes …
<% show_for @ person do | p | %> <% = p . attribute :first_name %> <% = p . attribute :last_name %> <% = p . attribute :confirmed ? %> <% = p . attribute :created_at , :format => :short %> <% = p . attribute :age , :if_blank => "No age" %> <% p . attribute :photo do %> <% = image_tag ( @ person. photo_url ) %> <% end %> <% end %>
Here is an example output you will get:
<div class="show_for person" …
…with a stick when things are going great. And getting an intro to Mr. Very Important Person before you have anything of material value is usually not going to give you much anyway.
5. Taking money means big exit or IPO
I'd argue the opposite. When you take money, your exit is bound to be smaller unless you're playing the Web 2.0 lottery game (where a few lucky contestants gets bought for sums completely uncorrelated to business fundamentals). Taking money means giving up equity, …
…involving modularity. In essence, Rob wants to be able to "decorate" the Person class to include teacher traits.
Nick's response would have been to create a factory that creates a Person proxy decorated with Teacher properties. And he would have been technically correct, but that description obscures the Ruby implementation, and makes it sound like we need new "Factory" and "Decorator" objects, as we do, in fact, need when programming in Java…
Here's the Person model with validations and serialization:
class Person include ActiveModel::Validations include ActiveModel::Serialization validates_presence_of :first_name , :last_name attr_accessor :attributes def initialize ( attributes = { } ) @ attributes = attributes end def read_attribute_for_validation ( key ) @ attributes [ key ] end end
Others
Those are just two of the modules available in ActiveModel. Some others include:
AttributeMethods : Makes it easy …