get bodies by tag

13 views
Skip to first unread message

Paulo de Souza Geyer

unread,
Aug 23, 2012, 7:09:22 PM8/23/12
to alavet...@googlegroups.com
Hi all,

I'm looking for the best way to fetch all public bodies that have a common tag, like a city name, found something to do this in public_body#list method, but I found it a bit confusing. There is an easier way?

If not, we should create a method in the PublicBody model for this, I think it would be easier for everyone, I can do this (since I need that working now) and submit a pull request, what do you think?

thanks,
Paulo

Louise Crow

unread,
Aug 24, 2012, 4:10:26 AM8/24/12
to alavet...@googlegroups.com
Hi Paulo,

The code in public_body#list seems to be handling a number of special cases:

* Combining a tag search with a query against the public body name
* Getting untagged bodies
* Getting bodies using machine tags (tags formatted as "key:value")
* Doing a search by the first letter of the public body name

For a simple search for all public bodies tagged with a particular
tag, you should be able to use:

PublicBody.find_by_tag('london')

If that doesn't cover what you need, do drop in a pull request that
does, and we'll get it merged in.

Cheers

Louise

Paulo de Souza Geyer

unread,
Aug 24, 2012, 2:36:45 PM8/24/12
to alavet...@googlegroups.com
oh, find_by_tag does exactly what I needed, thanks Louise!

I've added a list of cities into public_body_categories_en.rb, is there any way of getting only the tags inside the "Cities" section? I found out how to get all tags, but this won't work.

I can hardcode them in the HTML, but would be better just to change there to update the whole site

Louise Crow

unread,
Aug 28, 2012, 8:50:29 AM8/28/12
to alavet...@googlegroups.com
Hi Paulo,

There didn't seem to be an easy way to get tags by heading, so I've added one - if you check out the develop branch [1], you should be able to call PublicBodyCategories::get().by_heading() to get a hash of lists of tags, keyed by heading. So for you, the results should look something like

{'Cities' => ['london', 'paris'],
'Some other heading' => ['other tag', 'another tag']}

Let me know if that doesn't seem to do the trick.

Cheers

Louise


[1] https://github.com/sebbacon/alaveteli/
Reply all
Reply to author
Forward
0 new messages