-
Website
http://aimee.mychores.co.uk/ -
Original page
http://aimee.mychores.co.uk/2008/09/12/post/328 -
Subscribe
All Comments -
Community
-
Top Commenters
-
Cheap Textbooks
5 comments · 1 points
-
bondage
8 comments · 1 points
-
Traveller_Adventure
5 comments · 2 points
-
tkpandey
3 comments · 2 points
-
Affordable SEO
3 comments · 1 points
-
-
Popular Threads
-
Glimmers of Hope
4 days ago · 2 comments
-
The Lord's Prayer, for coders
2 weeks ago · 7 comments
-
Glimmers of Hope
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:
<pre>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</pre>
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