Skip to content

Instantly share code, notes, and snippets.

@LRDesign
Created August 11, 2008 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LRDesign/4816 to your computer and use it in GitHub Desktop.
Save LRDesign/4816 to your computer and use it in GitHub Desktop.
## Load a person, show that needs_loan == false
>> person = Person.find(18)
# ... snip ...
>> person.needs_loan
=> false
## Show that I can set needs_loan by hand and save
>> person.needs_loan = true
=> true
>> person.save!
=> true
>> person.needs_loan
=> true
## set it to false again
>> person.needs_loan = false
=> false
>> person.save!
=> true
## show that they are associated with an active loan
>> person.loan_requests.size
=> 4
>> person.loan_requests.any? { |l| l.active? }
=> true
## run the update_needs_loan! method to try to have the model set its
# own attribute to true
>> person.update_needs_loan!
=> true
>> person.needs_loan
=> false # It doesn't work. What gives?
class Person < ActiveRecord::Base
# needs_loan :boolean(1)
has_many :loan_requests
def update_needs_loan!
needs_loan = loan_requests.any? { |l| l.active? }
save!
end
end
class LoanRequest < ActiveRecord::Base
# status :string(255)
def active?
status == "active"
end
end
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment