Sara Trice
Just a programmin', bellydancin', cake bakin' kinda girl

  • RSS-Feed posts
  • RSS-Feed comments

  • gallery
  • livejournal
  • résumé
  • website portfolio

Pages

  • gallery
  • livejournal
  • résumé
  • website portfolio

Fun Stuff

  • My Amazon Wish List

Webcomics

  • Dark Legacy Comics
  • Erfworld
  • Questionable Content

New Posts

  • Indexing users that belong to groups with ancestry and thinking sphinx
  • Avoiding Ruby hash conditionals in Ruby on Rails
  • Quickie: How to add a blank option to options_from_collection_for_select
  • Using javan/whenever on Engine Yard Cloud
  • Illinois Home Bakeries – for farmer’s markets only

Latest Comments

  • 08.05 | Marc-André Lafortune in Testing CKEditor in Ruby on Rails with Cucumber/Ca…
  • 01.05 | Charles in ImageMagick: "convert: color profile operates on a…
  • 25.02 | Stanislaw in Testing CKEditor in Ruby on Rails with Cucumber/Ca…
  • 24.02 | admin in Avoiding Ruby hash conditionals in Ruby on Rails
  • 24.02 | Pam in Avoiding Ruby hash conditionals in Ruby on Rails
May 2012
S M T W T F S
« Mar    
 12345
6789101112
13141516171819
20212223242526
2728293031  
21
Mar

Indexing users that belong to groups with ancestry and thinking sphinx

So you’ve created a table of groups that are in a hierarchy with the gem ancestry, and you’ve created a table of users, and you’ve joined users to groups with a join table.

And now you want to use Thinking Sphinx, which is awesome for searching. So you think, hey, wouldn’t it be great if I could do a search for all users in a group’s subtree? Except that Thinking Sphinx cares not for your puny ancestry methods, and :subtree_ids doesn’t work in an index.

Fear not! I’ve done the heavy lifting for you.

in user.rb:
define_index do
  indexes last_name
  has groups(:id), :as => :direct_group_ids
  has "CONCAT_WS('/',groups.id,groups.ancestry)", :as => :group_ids, :type => :multi
end

The first attribute is so that the join is made from users to groups. The second actually creates the multi-value attribute that you can search on. So from there you can do:

User.search(:with_all => {:group_ids => [1]})

That will give you all the users that belong to the subtree, including the root group (in this case, the group with the id of “1″).

That being said, if you only want the users from sub-groups of the group you’re searching on (i.e. you never want users that are directly attached to the group you’re searching on), you can instead do this:

in user.rb:
define_index do
  indexes last_name
  has groups(:ancestry), :as => :group_ids, :type => :multi
end

So if you have group 1 which has group 2 and group 3 as children, the first example will give you all the users attached to all 3 groups; the second example will only give you the users attached to groups 2 and 3.

One last gotcha: if you’re running the search in console, remember to add “:per_page => 100″ or however many entries you want back, or else by default you only get 20. Don’t want you to headdesk when you can’t figure out why it’s returning 20 users when it’s supposed to be returning 75.

Happy indexing!

by admin in tech
no comment

9
Feb

Avoiding Ruby hash conditionals in Ruby on Rails

This gets really old:

if params[:teacher] && params[:teacher][:id] ...

so instead, do this:

if params[:teacher].try(:[], :id)

or do it a lot more:

name = params[:company][:owner][:name] if params[:company] and params[:company][:owner] and params[:company][:owner][:name]

turns into:

name = params.try(:[], :company).try(:[], :owner).try(:[], :name)

Yay for Stack Overflow!

by admin in tech
2 comments

28
Dec

Quickie: How to add a blank option to options_from_collection_for_select

<%= select_tag "some_select", ("<option></option>" + options_from_collection_for_select(@foo, "id", "item")).html_safe %>
by admin in tech
no comment

28
Sep

Using javan/whenever on Engine Yard Cloud

UPDATE 4.17.2012: If you’re on EY and not using custom chef recipes, you should start. A whenever recipe has been written and is detailed here. Info on how to start using custom chef recipes is here. It’s not so bad, really. One gotcha: your “appname” in the recipe is not your rails application name, but your EY application name – these can be different things!


Things I wish I’d have known:
1. It does not matter if whenever is in your gemfile, you have to SSH to your EY server and install whenever as such: sudo gem install whenever
2. It will probably complain that you don’t have i18n installed, so: sudo gem install i18n

EDIT: You will have to repeat the two steps above ANYTIME you restart your EY instance!

3. If you want your crontab updated on deploy, make a file named deploy/before_restart.rb and stick this in it (make sure you replace ‘yourAppNameHere’, derp):

@rails_env = node[:environment][:framework_env]
run "cd #{release_path}; whenever --set environment=#{@rails_env}; whenever --update-crontab 'yourAppNameHere'"

This is called a ‘deploy hook’ and here’s more info.

I found the idea for this in an old EY forum post.

by admin in tech
no comment

9
Sep

Illinois Home Bakeries – for farmer’s markets only

Interesting tidbit about a new law that is being passed. IL Senate Bill 840 allows for “Cottage food operations” (i.e. you can use your home kitchen), with several stipulations, if and only if you are selling goods at a farmers’ market. So if I wanted to sell my cakes at a farmer’s market and got the appropriate paperwork, I could, but I still couldn’t sell them to individuals specifically out of my home. I’d have to have a table at a farmer’s market. Which is pretty much useless to me. Oh well, it’s a step in the right direction.

Article from the News-Gazette
Article from the IL Stewardship Alliance
Senate bill 840

by admin in cakes
no comment

4
May

jsTree: adding Expand All and Collapse All buttons

The documentation for jsTree is thorough, but not particularly easy to read. If you are looking for an easy way to add “Expand All” and “Collapse All” buttons, here’s one way:

<input type="button" value="Collapse All" onclick="$('#tree_container_id').jstree('close_all');">
<input type="button" value="Expand All" onclick="$('#tree_container_id').jstree('open_all');">

where ‘#tree_container_id’ is, of course, the ID of the container node for your tree.

by admin in tech
1 comment

11
Apr

Uploadify: changing scriptData with dropdowns

Uploadify is a pretty awesome jQuery/flash uploader. It’s made even more awesome by the stuff you can do with it on the fly.

For example, if you want to pass a variable chosen from a dropdown via the uploader, you can use uploadifySettings() to do so. Some people appear to have problems with this part, so here’s how I did it. (I’m not going to show you all the settings, since the docs give you a pretty good idea how to set it up. )

Say you have a dropdown in your form with the id of ‘upload_type’:

<select id="upload_type" name="upload_type">
<option></option>
<option value="1">First type</option>
<option value="2">Second type</option>
</select>

In your jQuery $(document).ready function (but outside the $(‘#upload’).uploadify function), put something like this:

$('#upload_type').change(function() {
var type_val = $(this).val();
$('#upload').uploadifySettings("scriptData", {"upload_type" : type_val});
});

Which means “Whenever the form field identified with ‘upload_type’ changes, update the ‘upload_type’ variable in scriptData if it exists; if it does not already exist in scriptData, add it.”

Note: I’ve only tried this in Uploadify 2.1.4.

by admin in tech
2 comments

23
Mar

Music industry shifts to being a service? Good news, bad news…

Here’s an interesting read from Ars Technica:

Did file-sharing cause recording industry collapse? Economists say no

The most interesting paragraph I see in this article is this one:

So what is emerging is an increasingly “ephemeral” global music culture based not upon the purchasing of discrete physical packages of music, but on the discovery and subsequent promotion of musicians through file sharing. The big winner in this model is not the digital music file seller, but the touring band, whose music is easily discoverable on the ‘Net. As with so much of the rest of the emerging world economy, the shift is away from buying things and towards purchasing services—in this case tickets to concerts and related activities.

So I have to wonder – to my friends (and my husband) who count on selling merch at their unpaid performances – is this a game changer? Will musicians from now on be ultimately unable to make a living unless they go on the road – and stay there?

by admin in ramblings
no comment

9
Mar

Testing CKEditor in Ruby on Rails with Cucumber/Capybara

I have a textarea with the id of “request_details”, made into a CKEditor instance by jQuery: $(‘#request_details’).ckeditor();

(Check this page if you need some help getting CKEditor to work with jQuery, it’s dead simple)

Feature looks like this:  And I fill in "Here are some details" in the CKEditor instance "request_details"

Step looks like this:

When /^I fill in "([^"]*)" in the CKEditor instance "([^"]*)"$/ do |value, input_id|
browser = page.driver.browser
browser.execute_script("CKEDITOR.instances['#{input_id}'].setData('#{value}');")
end

by admin in tech
5 comments

23
Feb

My least favorite phrase

I’ve decided on my least favorite phrase in the English language:

“Can’t you just…?”

Why is this my least favorite phrase? Because:

  1. It’s always in conjunction with a task that the questioner does not understand, but assumes should not be difficult at all;
  2. it is inherently second-guessing the person being asked, which is insulting;
  3. and, rather than asking if something is possible, as in “Can you?” or “Could you?”, the speaker is really saying, “This must be possible, and if you can’t do it, you’re an idiot.”

If you don’t understand how the system works, or what is involved with doing a task, please, ask if something is possible. Don’t demean the craftsman or browbeat them into doing whatever it is you want. A resentful craftsman is not on your side, and will not go out of his/her way to help you.

by admin in ramblings, tech
2 comments

Next Page »

Meta

  • Log in
© 2008 - 2012
Design & CSS by
Freizeitler

Blogroll

    Dark Legacy Comics Yay MMORPGs!
    Erfworld Rob Balder writes this
    My Amazon Wish List Buy me somethin’!
    Questionable Content Am I more like Faye or Dora?