CouchRest Model find methods

285 views
Skip to first unread message

Pablo L. de Miranda

unread,
May 15, 2011, 8:21:55 PM5/15/11
to CouchRest
Hi guys,

I'm developing a system using Rails 3 and CouchDB with CouchRest Model
gem.
I'm try to use the find methods, so I defined my model like bellow:

class User < CouchRest::Model::Base
attr_accessor :password

property :name
property :email

view_by :name
end

But when I try to use User.by_name( :name => "part name") I never have
the right results, and when I try to use User.find_by_name the engine
results only return only 1 result as a Hash.
How can I get all the users by a part of they names, just like using
LIKE operator on SQL?

Thanks

Samuel Lown

unread,
May 16, 2011, 4:02:07 AM5/16/11
to couc...@googlegroups.com
Hi Pablo,

You have the right idea on creating the view, but it's usage is quite different to what you'd expect with an SQL query.

Check out the CouchDB view api for the basics on how views work and the options you can use:  http://wiki.apache.org/couchdb/HTTP_view_API 

In your example, you could find a specific user by doing either:

     User.by_name( :key => "name")
or
     User.find_by_name("name")

However, these are *not* LIKE expresions. To do that, as per CouchDB's API, you'd need to do:

    User.by_name(:startkey => 'name', :endkey => "name\u9999")

This will find all keys in the view that start with "name". Out of convention, the "\u9999" character is used so that CouchDB will match any character below it, you could just as easily have used "nameZ" if you don't think you'll have any non-english characters.

Of course, only the first part of the string will be matched. If you need to match from the other end (LIKE '%name%') you'd need to resort to a search engine as CouchDB does not support this with simple views. (It is possible though, create a huge index by splitting each key into substrings each starting with a different letter, google should have an example somewhere.)

Hope this helps, and don't forget to checkout the latest beta, views are a bit easier to use.

Cheers,
sam


--
You received this message because you are subscribed to the Google Groups "CouchRest" group.
To post to this group, send email to couc...@googlegroups.com.
To unsubscribe from this group, send email to couchrest+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/couchrest?hl=en.




--
www.samlown.com
www.autofiscal.com
www.planetaki.com/sam

Pablo L. de Miranda

unread,
May 16, 2011, 8:34:30 AM5/16/11
to couc...@googlegroups.com
Hi Sam,

Thank you so much for your help.
About the new Beta, I tried to use it on my dev box, but it need the
couchrest version 1.1.0.pre2 and it break all my tests every time that
I call database.recreate! I get a RestClient::ResourceNotFound. Thus,
I back to use the couchrest_model version 1.0.0 and couchrest 1.0.2.

Att,

Pablo Lacerda de Miranda
Software Engineer
Ci&T - Campinas/Brazil
pablol...@gmail.com
+55 11 8701-1086

Samuel Lown

unread,
May 16, 2011, 5:31:24 PM5/16/11
to couc...@googlegroups.com

About the new Beta, I tried to use it on my dev box, but it need the
couchrest version 1.1.0.pre2 and it break all my tests every time that
I call database.recreate! I get a RestClient::ResourceNotFound.

If you could provide any more details about this issue it would be a great help to try and resolve it. ResourceNotFound errors are usually to do with an invalid request, the CouchDB logs should provide the HTTP request sent to the database to be able to properly debug the issue.

There are no errors with this in the current tests so I'd expect you're doing something slightly different to the norm.

Cheers,
sam



--
www.samlown.com
www.autofiscal.com
www.planetaki.com/sam

Pablo L. de Miranda

unread,
May 16, 2011, 7:28:19 PM5/16/11
to couc...@googlegroups.com
Hi Sam,
As you ask me, I'm sending more information about the errors that I'm
getting when using couchrest 1.1.0pre2 and couchrest_model 1.1.0beta5,
I don't have any of this erros using the last stable versions of both
gems.
Bellow I'm sending you a test case example:

describe Subdomain do

before(:all) do
@database = CouchServer.default_database
end

after(:all) do
@database.delete!
end

before(:each) do
Subdomain.use_database @database
@attr = { :name => "teste", :responsable => "Pablo Lacerda",
:email => "pablol...@gmail.com", :password => "password",
:password_confirmation => "password" }
end

after(:each) do
@database.recreate!
end

it "should create a new instance with valid attributes" do
Subdomain.create!(@attr)
end

it "should have a name" do
@attr.merge!(:name => "")
subdomain = Subdomain.new(@attr)
subdomain.should_not be_valid
end

end

In the first test "should create a new instance with valid attributes" pass,
but on the second test "should have a name" I get the error like bellow:

1) Subdomain should have a name
Failure/Error: subdomain.should_not be_valid
RestClient::ResourceNotFound:
404 Resource Not Found
# ./spec/models/subdomain_spec.rb:28:in `block (2 levels) in <top
(required)>'

For the two test cases I have the follow lines on couchdb log:

[Mon, 16 May 2011 23:24:05 GMT] [info] [<0.11484.14>] 127.0.0.1 - -
'PUT' /oqg-test 201
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11512.14>] 127.0.0.1 - -
'PUT' /oqg-test 412
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11524.14>] 127.0.0.1 - -
'GET' /oqg-test/_design/Subdomain 404
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11525.14>] 127.0.0.1 - -
'PUT' /oqg-test/_design/Subdomain 201
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11526.14>] 127.0.0.1 - -
'GET' /oqg-test/_design/Subdomain/_view/by_nome?key=%22%22&limit=2&include_docs=false
200
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11527.14>] 127.0.0.1 - -
'GET' /oqg-test/_design/Subdomain 200
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11538.14>] 127.0.0.1 - -
'PUT' /oqg-test/_design/Subdomain 201
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11539.14>] 127.0.0.1 - -
'GET' /oqg-test/_design/Subdomain/_view/by_email?key=%22pablolmiranda%40gmail.com%22&limit=2&include_docs=false
200
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11541.14>] Shutting down
view group server, monitored db is closing.
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11528.14>] Shutting down
view group server, monitored db is closing.
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11540.14>] 127.0.0.1 - -
'DELETE' /oqg-test 200
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11551.14>] 127.0.0.1 - -
'PUT' /oqg-test 201
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11553.14>] 127.0.0.1 - -
'PUT' /oqg-test 412
[Mon, 16 May 2011 23:24:09 GMT] [info] [<0.11561.14>] 127.0.0.1 - -
'DELETE' /oqg-test 200
Att,

Pablo Lacerda de Miranda
Engenheiro de Software
Ci&T - Campinas
pablol...@gmail.com
+55 11 8701-1086

Samuel Lown

unread,
May 17, 2011, 3:50:34 AM5/17/11
to couc...@googlegroups.com
Hi,

Thanks for that. I think the problem is with the "@database.recreate!". Its deleting the database but not updating the cached state of the associated design documents. CouchRest is assuming the Design Documents haven't been touched so when the database is recreated they no longer exist.

I'll try and extend the associated CouchRest Database methods to delete the cache, but in the mean time, you might want to try adding the following to delete the cache in your tests:

    Thread.current[:couchrest_design_cache] = {}

Cheers,
sam

Pablo L. de Miranda

unread,
May 17, 2011, 5:36:10 AM5/17/11
to couc...@googlegroups.com
Sam,

Thank you, this solved my problem. Know I feel comfortable to use the new beta.

Regards,

Pablo

Reply all
Reply to author
Forward
0 new messages