On Thu, Oct 23, 2008 at 10:35 PM, Nick <[EMAIL PROTECTED]> wrote:

>
> Hi guys. I'm experiencing some strange behaviour in my Rails app. I
> have a constant, and two class methods:
>
> #----------#
> COORDINATES_NOT_NULL_SQL =
>  'latitude IS NOT NULL and longitude IS NOT NULL'
>
> def self.sql_for_non_null_coordinates
>  COORDINATES_NOT_NULL_SQL
> end
>
> def self.filtered_properties(params)
>  conditions_string = self.sql_for_non_null_coordinates
>  ...8<...
>  conditions_string << ' AND ' << filtered[:sql]
>  ...8<...
> end
> #----------#
>
> If I call #filtered_properties multiple times in a row, the value that
> #sql_for_non_null_coordinates returns changes:
> http://pastie.org/299037
>
> This line is causing the change:
>  conditions_string = self.sql_for_non_null_coordinates
> But why? And is it possible to prevent this behaviour?
>

Hi Nick,
This is because Ruby constants are not constants. They can change.
Ruby warns you when you reinitialize a constant, but not otherwise.
That is,
Constant << "anything"
won't produce a warning.

You should use something like,
conditions_string  = self.sql_for_non_null_coordinates.dup
....


-- 
- Kazim Zaidi
Blog: http://tuxplayground.blogspot.com

--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to