Created
August 11, 2008 06:25
-
-
Save LRDesign/4816 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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