List Info

Thread: alias_method_chain and ActiveRecord Attributes




alias_method_chain and ActiveRecord Attributes
country flaguser name
United States
2007-10-09 12:40:59
I discovered something interesting today on Edge Rails: you
can't use
alias_method_chain on ActiveRecord attributes.

I wanted to set a filter on a boolean attribute to prevent
it from
becoming false when it is already true, but the opposite is
allowed.
Try this:

class Something < ActiveRecord::Base
  # Assume some boolean attribute named 'is_cool' is
defined

  def is_cool_with_state_check=(new_state)
    # ... Doesn't matter
  end

  alias_method_chain :is_cool, :state_check
end

If you run your tests you'll find that your class
completely
explodes.  Can anyone else verify this?

James Herdman


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: alias_method_chain and ActiveRecord Attributes
country flaguser name
United States
2007-10-09 12:52:28
For future Googlers, the way I got around this was to just
re-define
the setter and pass it to super.  For example:

# Using our previous 'is_cool' boolean attribute
def is_cool=(new_state)
  super if some_logical_check?
end

On Oct 9, 1:40 pm, "James H."
<james.herd...gmail.com> wrote:
> I discovered something interesting today on Edge Rails:
you can't use
> alias_method_chain on ActiveRecord attributes.
>
> I wanted to set a filter on a boolean attribute to
prevent it from
> becoming false when it is already true, but the
opposite is allowed.
> Try this:
>
> class Something < ActiveRecord::Base
>   # Assume some boolean attribute named 'is_cool' is
defined
>
>   def is_cool_with_state_check=(new_state)
>     # ... Doesn't matter
>   end
>
>   alias_method_chain :is_cool, :state_check
> end
>
> If you run your tests you'll find that your class
completely
> explodes.  Can anyone else verify this?
>
> James Herdman


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


Re: alias_method_chain and ActiveRecord Attributes
country flaguser name
United States
2007-10-10 12:00:26
James,

I've dealt with a similar issue recently.  I believe that
the issue is
that alias_method_chain must be called _after_ the method
exists on
the class.  Accessors are not created for instance
attributes when an
instance is loaded, but are created via method_missing.  As
such they
are too late in the game to be decorated with
alias_method_chain.

Your workaround solves the issue by creating the accessor as
part of
the class definition.  In that way it's available early
enough for
alias_method_chain to latch on to.  I'm not certain, but I
believe you
could accomplish the same thing with a call to
attr_accessor.


AndyV

On Oct 9, 1:52 pm, "James H."
<james.herd...gmail.com> wrote:
> For future Googlers, the way I got around this was to
just re-define
> the setter and pass it to super.  For example:
>
> # Using our previous 'is_cool' boolean attribute
> def is_cool=(new_state)
>   super if some_logical_check?
> end
>
> On Oct 9, 1:40 pm, "James H."
<james.herd...gmail.com> wrote:
>
> > I discovered something interesting today on Edge
Rails: you can't use
> > alias_method_chain on ActiveRecord attributes.
>
> > I wanted to set a filter on a boolean attribute to
prevent it from
> > becoming false when it is already true, but the
opposite is allowed.
> > Try this:
>
> > class Something < ActiveRecord::Base
> >   # Assume some boolean attribute named 'is_cool'
is defined
>
> >   def is_cool_with_state_check=(new_state)
> >     # ... Doesn't matter
> >   end
>
> >   alias_method_chain :is_cool, :state_check
> > end
>
> > If you run your tests you'll find that your class
completely
> > explodes.  Can anyone else verify this?
>
> > James Herdman


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails" group.
To post to this group, send email to rubyonrailsgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-unsubscribegooglegroups.com
For more options, visit this group at http://gro
ups.google.com/group/rubyonrails
-~----------~----~----~----~------~----~------~--~---


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )