From asa at caltech.edu Wed Aug 9 17:41:06 2006 From: asa at caltech.edu (Asa Hopkins) Date: Wed, 9 Aug 2006 14:41:06 -0700 Subject: [Ez-where-devel] Serializing EZ::Where objects for storage Message-ID: Ezra et al., I'm a big fan of several of the plugins you've made available, including your nice ez-where plugin for searches. Thanks! I'm trying to devise a scheme whereby users of my Rails app (a voter file manager) will be able to construct arbitrary queries (eg for picking out subsets of voters to target). I'd like to be able to store serialized EZ::Where::Condition objects in the database, and then the users can combine them to build more complicated queries. i have no problem declaring the serialized object, and when I put an EZ::Where::Condition in there, I get a reasonable looking YAML text in the database. For example, this is for an object that searches the table people for name.nocase == 'Asa' --- !ruby/object:EZ::Where::Condition clauses: - !ruby/object:EZ::Where::Clause case_insensitive: true name: name negate: false table_prefix: people. test: :equals value: Asa context_binding: !ruby/object:Binding {} inner: :and outer: :and parenthesis: table_name: :people However, when I try to get the object back out (eg for a .to_sql method), I get the following complaint: "ActiveRecord::SerializationTypeMismatch: object was supposed to be a EZ::Where::Condition, but was a String" If I try to YAML::load that object, I get "TypeError: allocator undefined for Binding from /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer' from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load'" Any ideas on how I can store a conditions object for the future, either by fixing this serialization process, or by storing some set of rules necessary to recreate a Condition? I could imagine storing Clauses instead, if that might work better. Thanks, Asa ----------------- Work is love made visible ----------------- Asa S. Hopkins MC 266-33, Pasadena, CA 91125 asa at caltech.edu http://www.its.caltech.edu/~asa/ http://www.opencampaigns.net ----------------- From asa at caltech.edu Wed Aug 9 18:56:05 2006 From: asa at caltech.edu (Asa Hopkins) Date: Wed, 9 Aug 2006 15:56:05 -0700 Subject: [Ez-where-devel] Serializing EZ::Where objects for storage In-Reply-To: References: Message-ID: <499375AC-F525-42E7-A71C-3B521F9D69CE@caltech.edu> Two solutions I've found so far: 1) Don't declare your Condition initially with a block. If you initialize it with a block, and then save it in the database, you save some sort of binding information (which I admit not understanding at all), which then can't be read back out. That is, if you have a model class ARObject < ActiveRecord::Base serialize :text_field, EZ::Where::Condition end and then do this: cond = EZ::Where::Condition.new :my_table do foo == 'bar' my_table :outer => :or do baz == 'fuzz' biz == 'boz' end end ARObject.text_field = cond ARObject.save then you'll end up not being able to get your object back. If you change the type of Clause you have, and go instead to an ArrayClause, by doing cond.to_sql.to_c, then save it, you're fine. 2) Don't serialize EZ::Where::Conditions in the database; serialize an array that can be turned into a Condition object by .to_condition Asa On Aug 9, 2006, at 2:41 PM, Asa Hopkins wrote: > Ezra et al., > > I'm a big fan of several of the plugins you've made available, > including your nice ez-where plugin for searches. Thanks! > > I'm trying to devise a scheme whereby users of my Rails app (a voter > file manager) will be able to construct arbitrary queries (eg for > picking out subsets of voters to target). I'd like to be able to > store serialized EZ::Where::Condition objects in the database, and > then the users can combine them to build more complicated queries. i > have no problem declaring the serialized object, and when I put an > EZ::Where::Condition in there, I get a reasonable looking YAML text > in the database. > > For example, this is for an object that searches the table people for > name.nocase == 'Asa' > --- !ruby/object:EZ::Where::Condition > clauses: > - !ruby/object:EZ::Where::Clause > case_insensitive: true > name: name > negate: false > table_prefix: people. > test: :equals > value: Asa > context_binding: !ruby/object:Binding {} > > inner: :and > outer: :and > parenthesis: > table_name: :people > > However, when I try to get the object back out (eg for a .to_sql > method), I get the following complaint: > "ActiveRecord::SerializationTypeMismatch: object was supposed to be a > EZ::Where::Condition, but was a String" > > If I try to YAML::load that object, I get > "TypeError: allocator undefined for Binding > from /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer' > from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load'" > > Any ideas on how I can store a conditions object for the future, > either by fixing this serialization process, or by storing some set > of rules necessary to recreate a Condition? I could imagine storing > Clauses instead, if that might work better. > > Thanks, > Asa > > ----------------- > Work is love made visible > ----------------- > Asa S. Hopkins > MC 266-33, Pasadena, CA 91125 > asa at caltech.edu > http://www.its.caltech.edu/~asa/ > http://www.opencampaigns.net > ----------------- > > _______________________________________________ > Ez-where-devel mailing list > Ez-where-devel at rubyforge.org > http://rubyforge.org/mailman/listinfo/ez-where-devel ----------------- Work is love made visible ----------------- Asa S. Hopkins MC 266-33, Pasadena, CA 91125 asa at caltech.edu http://www.its.caltech.edu/~asa/ ----------------- -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ez-where-devel/attachments/20060809/2594888d/attachment-0001.html From ezmobius at gmail.com Wed Aug 9 19:24:57 2006 From: ezmobius at gmail.com (Ezra Zygmuntowicz) Date: Wed, 9 Aug 2006 16:24:57 -0700 Subject: [Ez-where-devel] Serializing EZ::Where objects for storage In-Reply-To: <499375AC-F525-42E7-A71C-3B521F9D69CE@caltech.edu> References: <499375AC-F525-42E7-A71C-3B521F9D69CE@caltech.edu> Message-ID: <22A0B22F-4BD4-4288-BF33-14CD31020BB9@gmail.com> Hey Asa- Looks like you figured out what I was going to tell you ;) Ez-where uses all kinds of blocks, procs and Binding objects to do its magick. ANd you cannot serialize a block, proc or Binding object. So you need to convert to something that can be serialized, like strings or array clauses. You ended up with exactly what I would have told you to do. Cheers- -Ezra On Aug 9, 2006, at 3:56 PM, Asa Hopkins wrote: > Two solutions I've found so far: > > 1) Don't declare your Condition initially with a block. If you > initialize it with a block, and then save it in the database, you > save some sort of binding information (which I admit not > understanding at all), which then can't be read back out. > > That is, if you have a model > class ARObject < ActiveRecord::Base > serialize :text_field, EZ::Where::Condition > end > > and then do this: > cond = EZ::Where::Condition.new :my_table do > foo == 'bar' > my_table :outer => :or do > baz == 'fuzz' > biz == 'boz' > end > end > > ARObject.text_field = cond > ARObject.save > > then you'll end up not being able to get your object back. > > If you change the type of Clause you have, and go instead to an > ArrayClause, by doing cond.to_sql.to_c, then save it, you're fine. > > 2) Don't serialize EZ::Where::Conditions in the database; serialize > an array that can be turned into a Condition object by .to_condition > > Asa > > > On Aug 9, 2006, at 2:41 PM, Asa Hopkins wrote: > >> Ezra et al., >> >> I'm a big fan of several of the plugins you've made available, >> including your nice ez-where plugin for searches. Thanks! >> >> I'm trying to devise a scheme whereby users of my Rails app (a voter >> file manager) will be able to construct arbitrary queries (eg for >> picking out subsets of voters to target). I'd like to be able to >> store serialized EZ::Where::Condition objects in the database, and >> then the users can combine them to build more complicated queries. i >> have no problem declaring the serialized object, and when I put an >> EZ::Where::Condition in there, I get a reasonable looking YAML text >> in the database. >> >> For example, this is for an object that searches the table people for >> name.nocase == 'Asa' >> --- !ruby/object:EZ::Where::Condition >> clauses: >> - !ruby/object:EZ::Where::Clause >> case_insensitive: true >> name: name >> negate: false >> table_prefix: people. >> test: :equals >> value: Asa >> context_binding: !ruby/object:Binding {} >> >> inner: :and >> outer: :and >> parenthesis: >> table_name: :people >> >> However, when I try to get the object back out (eg for a .to_sql >> method), I get the following complaint: >> "ActiveRecord::SerializationTypeMismatch: object was supposed to be a >> EZ::Where::Condition, but was a String" >> >> If I try to YAML::load that object, I get >> "TypeError: allocator undefined for Binding >> from /usr/local/lib/ruby/1.8/yaml.rb:133:in `transfer' >> from /usr/local/lib/ruby/1.8/yaml.rb:133:in `load'" >> >> Any ideas on how I can store a conditions object for the future, >> either by fixing this serialization process, or by storing some set >> of rules necessary to recreate a Condition? I could imagine storing >> Clauses instead, if that might work better. >> >> Thanks, >> Asa >> >> ----------------- >> Work is love made visible >> ----------------- >> Asa S. Hopkins >> MC 266-33, Pasadena, CA 91125 >> asa at caltech.edu >> http://www.its.caltech.edu/~asa/ >> http://www.opencampaigns.net >> ----------------- >> >> _______________________________________________ >> Ez-where-devel mailing list >> Ez-where-devel at rubyforge.org >> http://rubyforge.org/mailman/listinfo/ez-where-devel > > ----------------- > Work is love made visible > ----------------- > Asa S. Hopkins > MC 266-33, Pasadena, CA 91125 > asa at caltech.edu > http://www.its.caltech.edu/~asa/ > ----------------- > > > _______________________________________________ > Ez-where-devel mailing list > Ez-where-devel at rubyforge.org > http://rubyforge.org/mailman/listinfo/ez-where-devel -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/ez-where-devel/attachments/20060809/47ca2895/attachment.html