A little place of calm: A little place of calm » Blog Archive » CouchDB on Rails (part 6 of ?)
PaulCarey
· 1 year ago
Hi Aimee
I'll echo what Jan said - this is a great series.
I'm glad you got RelaxDB running on rails. I'm sorry I can't offer any advice as to where you should configure the connection, but you know more than I do at this point!
> It’s a shame i have to [add properties to a model]
That's interesting, I hadn't considered the need for adding properties dynamically. You could do it with module_eval or instance.class.instance_eval but I'm guessing that's not quite what you were looking for.
> To order them by artist then title, we can do this:
You've probably also seen that sorted_by yields a query object that maps directly to CouchDB query params.
> Sometimes it lets it through even when the validation blatantly fails.
Hmm, that's not so good. If you have an example of where it fails, please send it on.
Looking forward to the next installment. Paul
aimee
· 1 year ago
Hi Paul, thanks for your thoughts. I do think that dynamic properties are a major strength of CouchDB. I guess you have played with the web interface that comes with CouchDB? You can just add as many fields as you like, with any name you like. Each document can have an entirely different set of fields, if it wishes.
I think the couchrest plugin allows dynamic fields without any need for module_eval.
The validation fails as follows. Here is the model:
class Cd < RelaxDB::Document property :title, :validator => lambda {|t| t != ""}, :validation_msg => "cannot be blank" property :artist, :validator => lambda {|t| t != ""}, :validation_msg => "cannot be blank" property :publisher property :year_of_release, :validator => lambda {|p| p.to_i > 0}, :validation_msg => "must be a number" end
If i try to save a new empty CD it gives me all three error messages. If i then fill in just the year_of_release, leaving title and artist both blank, it saves.
If i remove the year_of_release validation (so that it just validates title and artist) then it works as expected and doesn't let me save until i enter both a title and an artist.
To be honest, maybe i got the validation wrong. I wrote it last night when i was tired and in a hurry to finish the blog post. Now that i look at it, i realise that it is not actually converting the value at all. So if i enter "123bubbles" it will probably pass the validation but store "123bubbles" in the database! Oops! How do you recommend validating a number?
PaulCarey
· 1 year ago
Thanks for the example. As you observed, validation isn't great.
The reason validation wasn't working for title or artist is because nil != "" is true. You probably want something like lambda {|t| not t.blank? } for your strings.
To ensure that a string gets converted to a number, things get ugly :) before_save lambda { |o| o.year_of_release = o.year_of_release.to_i } property :year_of_release, :validator => lambda {|p| p > 1908}, :validation_msg => "must be > 1908"
I'll probably add something like property, :foo, :is =>Fixnum over the coming week, but I want to think about how that ties in with callbacks and validation first.
If you really want to dynamically define properties, one approach would be to subclass RelaxDB::Document and define method_missing so it invoked property on the class.
Randy
· 1 year ago
I played around with CouchDb (via CouchDBX on the Mac) and RelaxDb. I got it working very quicky, thanks to Aimee's excellent walk-through and the total ease of running CouchDBX on the Mac.
I didn't try validations, so I can't comment on that aspect of it.
One thing that I noticed was that properties are not inherited via subclasses. That would be a nice feature in my opinion.
I also see both sides of the argument, concerning a schema-less model vs the need to define properties in the model (ala DataMapper).
I think CouchDb is fantastically powerful and full of potential. However, I can't wrap my mind past the views and the reduce. However, seeing how RelaxDb uses those views to make some easy to use relationships, properties, and searching for those properties, makes a lot of sense.
There's an interesting balance to find in RelaxDb. Easy to use for the Rails ActiveRecord guys (like me) versus the flexibility available to CouchDb (via map/reduce and views). Personally, I'd love to throw a whole bunch of hash like structs into couchdb, and then search, modify, and aggregate those documents, without having to wrap my mind around map/reduce. But, that's just me.
aimee
· 1 year ago
@Paul, wow, it's great to have you following along and responding to the particular issues that i've encountered. Thank you for the helpful hints about validations. That makes perfect sense to me. The ability to ensure properties are of a particular type will be very useful, i am sure.
@Randy, great! I'm so pleased that you found my notes helpful and got things working okay. To get me started i want something similar to ActiveRecord, and RelaxDB was certainly a good help. Next i'm going to move on to the Ruby libraries couchrest and couchobject, which i think will give me a little more flexibility.
Bruno
· 1 year ago
Hi Aimee. I haven't seen a example of new or edit template. I'm very curious about how to create forms with relaxdb, since the usual way is not working
Laurynas
· 11 months ago
Hello. Thanks for sharing your thoughts. It was helpful article for me. Thanks.
I'll echo what Jan said - this is a great series.
I'm glad you got RelaxDB running on rails. I'm sorry I can't offer any advice as to where you should configure the connection, but you know more than I do at this point!
(I've pasted code related to this reply on http://friendpaste.com/565hyrmA)
> It’s a shame i have to [add properties to a model]
That's interesting, I hadn't considered the need for adding properties dynamically. You could do it with module_eval or instance.class.instance_eval but I'm guessing that's not quite what you were looking for.
> To order them by artist then title, we can do this:
You've probably also seen that sorted_by yields a query object that maps directly to CouchDB query params.
> Sometimes it lets it through even when the validation blatantly fails.
Hmm, that's not so good. If you have an example of where it fails, please send it on.
Looking forward to the next installment.
Paul
I think the couchrest plugin allows dynamic fields without any need for module_eval.
The validation fails as follows. Here is the model:
If i try to save a new empty CD it gives me all three error messages. If i then fill in just the year_of_release, leaving title and artist both blank, it saves.
If i remove the year_of_release validation (so that it just validates title and artist) then it works as expected and doesn't let me save until i enter both a title and an artist.
To be honest, maybe i got the validation wrong. I wrote it last night when i was tired and in a hurry to finish the blog post. Now that i look at it, i realise that it is not actually converting the value at all. So if i enter "123bubbles" it will probably pass the validation but store "123bubbles" in the database! Oops! How do you recommend validating a number?
The reason validation wasn't working for title or artist is because nil != "" is true. You probably want something like lambda {|t| not t.blank? } for your strings.
To ensure that a string gets converted to a number, things get ugly :)
before_save lambda { |o| o.year_of_release = o.year_of_release.to_i }
property :year_of_release, :validator => lambda {|p| p > 1908}, :validation_msg => "must be > 1908"
I'll probably add something like
property, :foo, :is =>Fixnum
over the coming week, but I want to think about how that ties in with callbacks and validation first.
If you really want to dynamically define properties, one approach would be to subclass RelaxDB::Document and define method_missing so it invoked property on the class.
I didn't try validations, so I can't comment on that aspect of it.
One thing that I noticed was that properties are not inherited via subclasses. That would be a nice feature in my opinion.
I also see both sides of the argument, concerning a schema-less model vs the need to define properties in the model (ala DataMapper).
I think CouchDb is fantastically powerful and full of potential. However, I can't wrap my mind past the views and the reduce. However, seeing how RelaxDb uses those views to make some easy to use relationships, properties, and searching for those properties, makes a lot of sense.
There's an interesting balance to find in RelaxDb. Easy to use for the Rails ActiveRecord guys (like me) versus the flexibility available to CouchDb (via map/reduce and views). Personally, I'd love to throw a whole bunch of hash like structs into couchdb, and then search, modify, and aggregate those documents, without having to wrap my mind around map/reduce. But, that's just me.
@Randy, great! I'm so pleased that you found my notes helpful and got things working okay. To get me started i want something similar to ActiveRecord, and RelaxDB was certainly a good help. Next i'm going to move on to the Ruby libraries couchrest and couchobject, which i think will give me a little more flexibility.
you had this problem?
>> Cd.all
RuntimeError: 500:Internal Server Error
METHOD:GET
URI:/cd_collection/_view/Cd/all?reduce=false
{"error":"query_parse_error","reason":"Bad URL query key:reduce"}
madrugao
the bug was in view path and map/emit function for CouchDB 0.9.0a
fix
http://github.com/madrugao/relaxdb/tree/abe62f7...
It is really a big help especially for me that I'm a newbie in this...
Great post..
Thanks
Regards