Trouble with casting?

18 views
Skip to first unread message

Chad A.

unread,
Nov 3, 2009, 10:00:30 PM11/3/09
to CouchRest
Hi,

I'm having a bit of trouble getting couchrest to work with input from
a complicated form. Keeping it close to the metal, I'm only using
couch rest - no other couch helpers on a rails app. So I create two
documents

class Contact < CouchRest::ExtendedDocument
property :telephone_numbers, :cast_as => ['Telephone'], :default
=> []
end

class Telephone < CouchRest::ExtendedDocument
property :number, :type => 'String'
property :location, :type => 'String'
end

As I understand it, this means that couchrest will save an array of
hashes in the db - as json - that each will bemarked with couch-rest-
type called 'Telephone' so that when one is retrieved it is cast as a
Telephone document that has the accessor on it.

I'm just trying to figure out how to get couchrest to put the
telephone numbers correcting in the db. For instance, by hand, I make
a form, the rails way, and I nested the telephone_numbers in an array
like so...

<input type="text" name="contact[telephone_numbers][0][telephone]
[number]/>
<input type="text" name="contact[telephone_numbers][0][telephone]
[type]/>
<input type="text" name="contact[telephone_numbers][1][telephone]
[number]/>
<input type="text" name="contact[telephone_numbers][1][telephone]
[type]/>

Problem is that I don't get the results I expect. It does not put
json objects into a telephone_numbers array in couchdb...it simply
concatenates the fields and the values (hash keys/values), and puts
them in a array in couchdb like so...

[
{
"0": null,
"couchrest-type": "Telephone",
"telephonetypeworktelephonenumber2122225555": null
},
]

I get pretty much the same result if do this, if I removed the
telephone from the input...like so...
<input type="text" name="contact[telephone_numbers][0][number]/>


Am I missing something here? I was expecting a json object, not a
munged string. Or am I misunderstanding casting, etc?

Thanks for any advice,

Chad

Nicholas Orr

unread,
Nov 3, 2009, 10:08:54 PM11/3/09
to couc...@googlegroups.com
This works for me.

  class Telephone < Hash
    include CouchRest::CastedModel
    ....
  end

Nicholas Orr

unread,
Nov 4, 2009, 12:35:31 AM11/4/09
to couc...@googlegroups.com
I forgot a key part...

class Telephone < Hash
  include CouchRest::CastedModel
  # You have to add a casted_by accessor if you want to reach a casted extended doc parent
  attr_accessor :casted_by

end

Peter Gumeson

unread,
Nov 4, 2009, 1:22:25 PM11/4/09
to couc...@googlegroups.com
Actually, casted_by is now defined for you inside of CastedModel:
http://github.com/couchrest/couchrest/blob/master/lib/couchrest/more/casted_model.rb#L10

There is also now a property called base_doc on all ExtendedDocuments
and CastedModels that that will always give you the top level
(extended) document of any hierarchy.

Peter
--
Peter Gumeson
gum...@gmail.com

Chad A.

unread,
Nov 5, 2009, 10:03:15 AM11/5/09
to CouchRest
Thanks for the help. This appears to work when I manually create a
Telephone object and then add them to the telephone_numbers of the
Contact object.

Part of my issue was my own fault as well...rails indexed fields in
a form - contact[telephone_numbers][0][telephone][number] - create
hashes, not a array with a hash..I forgot about that. I'm going to
checkout the couchrest-rails stuff to see if it has an elegant
solution to this - rather than me manually converting a hash of hashes
into an array of hashes.

Thanks!

Chad

On Nov 4, 12:35 am, Nicholas Orr <nicholas....@zxgen.net> wrote:
> I forgot a key part...
>
> class Telephone < Hash
>   include CouchRest::CastedModel
>   # You have to add a casted_by accessor if you want to reach a casted
> extended doc parent
>   attr_accessor :casted_by
>
> end
>
> On Wed, Nov 4, 2009 at 2:08 PM, Nicholas Orr <nicholas....@zxgen.net> wrote:
> > This works for me.
>
> >   class Telephone < Hash
> >     include CouchRest::CastedModel
> >     ....
> >   end
>
Reply all
Reply to author
Forward
0 new messages