Like Query with a URL Type

62 views
Skip to first unread message

Neil C

unread,
Aug 22, 2012, 12:45:52 PM8/22/12
to datam...@googlegroups.com
I am trying to do the following:

person.photos.all(:url.like => "amazon").destroy

As you might guess, 1 person has n photos, and each photo has a url property. I am trying to remove any photos with "amazon" in the URL.

When I do this, I get the following:

NoMethodError: undefined method `gsub' for #<Addressable::URI:0x3fc672305d40 URI:amazon>

Any insight into solving this is appreciated.

Thanks.

Neil C

unread,
Aug 22, 2012, 12:48:23 PM8/22/12
to datam...@googlegroups.com
Besides, obviously, a SQL solution.

Thanks.

Abe Voelker

unread,
Aug 22, 2012, 1:01:05 PM8/22/12
to datam...@googlegroups.com
Is this too SQL-ish?

person.photos.all(:conditions => ['"url" ilike ?', "%amazon%"])

Abe Voelker

unread,
Aug 22, 2012, 1:14:19 PM8/22/12
to datam...@googlegroups.com
Also from your example it looks like DM is pulling in the url field as an Addressable::URI, so it is probably already fetching all the associated records from the DB in order to load them into memory to parse into Addressable::URI structures.  If that's the case, then I don't think there would be any additional performance overhead from what you originally had to just doing

person.photos.all.select{|photo| photo.url.host.downcase == 'amazon.com'}

Chris Corbyn

unread,
Aug 22, 2012, 6:52:20 PM8/22/12
to datam...@googlegroups.com
Way less readable and elegant though ;)  This is a bug.  DataMapper should know that URI properties are dumped to strings, so that should be what it does when it generates the query.

That said, your original query is wrong.  It should have the '%' marks around the search string:

person.photos.all(:url.like => '%amazon%').destroy

What version of DM are you using?  I just tried this with a model we use URI types on and it worked as expected.  I tried it both directly on the model, and by going through a 1:m relationship. (DM 1.2.0).


--
You received this message because you are subscribed to the Google Groups "DataMapper" group.
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/K0BzmeduifoJ.
To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.

Neil C

unread,
Aug 23, 2012, 9:53:33 PM8/23/12
to datam...@googlegroups.com
I caught the typos with the % sign after I posted. Still, as you said, that wouldn't cause the error but rather just not destroy anything.

I am using DM_VERSION = "~> 1.2.0". Basically, I cut and pasted what's in the dm-rails. I am not sure how you managed to make it work.

I have things working in a pure SQL way now, but any insight will be helpful.

Thanks.

Arthur M. Meskelis

unread,
Sep 4, 2012, 10:37:15 AM9/4/12
to datam...@googlegroups.com
Hi Neil C

I try to redo your case, so here:

ruby-1.9.3-p0 :022 > Person.first
 => #<Person @id=1 @name="arthur"> 
ruby-1.9.3-p0 :023 > Person.first.photos
 => [#<Photo @id=2 @url="b skeleton" @person_id=1>, #<Photo @id=3 @url="c wizard" @person_id=1>, #<Photo @id=4 @url="a amazon" @person_id=1>] 
ruby-1.9.3-p0 :024 > Person.first.photos.all(:url.like => '%amazon%')
 => [#<Photo @id=4 @url="a amazon" @person_id=1>] 
ruby-1.9.3-p0 :025 > Person.first.photos.all(:url.like => '%amazon%').destroy
 => true 
ruby-1.9.3-p0 :026 > Person.first.photos.all(:url.like => '%amazon%')
 => [] 
ruby-1.9.3-p0 :027 > Person.first.photos.all
 => [#<Photo @id=2 @url="b skeleton" @person_id=1>, #<Photo @id=3 @url="c wizard" @person_id=1>] 
ruby-1.9.3-p0 :028 > Photo.all
 => [#<Photo @id=2 @url="b skeleton" @person_id=1>, #<Photo @id=3 @url="c wizard" @person_id=1>] 
ruby-1.9.3-p0 :029 > 

Apparently it'd functioned. Is that what you want ?
Anyway, I'm using datamapper 1.1.0

so long

2012/8/23 Neil C <ne...@vidyasource.com>
To view this discussion on the web visit https://groups.google.com/d/msg/datamapper/-/tu1FnD7DyuwJ.

To post to this group, send email to datam...@googlegroups.com.
To unsubscribe from this group, send email to datamapper+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/datamapper?hl=en.



--
------------------------------------------
 1. Notebook para você ficar livre e programar a vontade: R$ 2300
 2. Curso de Programação/SysAdmin/DBA: R$ 5000
 3. Pedir a solução pronta para um problema numa lista de discussão: Não tem preço !

E para todas as outras existe RTFM, STFW e  LMGTFY
Reply all
Reply to author
Forward
0 new messages