# Alias\_method\_chain on a boolean attribute not calling

**URL:** <https://www.ruby-forum.com/t/alias-method-chain-on-a-boolean-attribute-not-calling/181085>\
**Category:** Rails\
**Created:** [January 5, 2010, 7:42am UTC](https://www.ruby-forum.com/t/alias-method-chain-on-a-boolean-attribute-not-calling/181085 "2010-01-05T07:42:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Curtis\_Jennings\_Sch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@Curtis\_Jennings\_Sch](https://www.ruby-forum.com/u/Curtis_Jennings_Sch)\
**Post date:** [January 5, 2010, 7:42am UTC](https://www.ruby-forum.com/t/alias-method-chain-on-a-boolean-attribute-not-calling/181085/1 "2010-01-05T07:42:29Z")

</div>

I have the below code - I cannot use alias\_method\_chain unless I  
declare :accepted= before using it - i suppost the attribute methods  
are added latter - I suspect that this case is blowing away my  
alias\_method\_chain

How can i complete this intention correctly, doing it the rails-way and  
pretty?

Thanks  
Curtis.

class IncomingVideo \< ActiveRecord::Base  
validates\_presence\_of :content\_url  
validates\_presence\_of :title

def uncurated?  
!accepted?  
end  
def accepted=(val)  
update\_attribute(:accepted,val)  
end

def accepted\_with\_create\_curated\_video=(val)  
if uncurated? and val == true  
cv = CuratedVideo.create(:title =\> title,  
:description =\> description,  
:content\_url =\> content\_url,  
:site\_url =\> site\_url)  
cv.save!  
end  
accepted\_without\_create\_curated\_video=val  
end  
alias\_method\_chain :accepted=,:create\_curated\_video  
end

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Curtis\_Jennings\_Sch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@Curtis\_Jennings\_Sch](https://www.ruby-forum.com/u/Curtis_Jennings_Sch)\
**Post date:** [January 6, 2010, 5:41am UTC](https://www.ruby-forum.com/t/alias-method-chain-on-a-boolean-attribute-not-calling/181085/2 "2010-01-06T05:41:51Z")

</div>

On Jan 5, 1:40Â am, Curtis Jennings Schofield  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the Â below code - I cannot use alias\_method\_chain unless I  
> declare :accepted= before using it - i suppost the attribute methods  
> are added latter - I suspect that this case is blowing away my  
> alias\_method\_chain
> 
> How can i complete this intention correctly, doing it the rails-way and pretty?

You’ll likely want to do this in an after\_save callback, as the method  
you’ve posted (if it worked) would end up drooling stray CuratedVideo  
records into the DB every time a model set that value (perhaps via  
mass-assignment) then didn’t pass validation.

–Matt J.

---

<div class="post-metadata">

**Author:** ![Curtis\_Jennings\_Sch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bbe5ce/32.png) [@Curtis\_Jennings\_Sch](https://www.ruby-forum.com/u/Curtis_Jennings_Sch)\
**Post date:** [January 6, 2010, 6:28am UTC](https://www.ruby-forum.com/t/alias-method-chain-on-a-boolean-attribute-not-calling/181085/3 "2010-01-06T06:28:05Z")

</div>

On Tue, Jan 5, 2010 at 9:40 PM, Matt J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 
> 
> You’ll likely want to do this in an after\_save callback, as the method  
> you’ve posted (if it worked) would end up drooling stray CuratedVideo  
> records into the DB every time a model set that value (perhaps via  
> mass-assignment) then didn’t pass validation.
> 
> –Matt J.

Thanks for your response - after i realized that it wasn’t going to  
work - i did use a  
call back and the \_changed? and \_was parts of the dirty attribute and  
it was as clean as i prefer and passed spec (and as you said - avoided  
extra useless queries).
