Pages
Fun Stuff
Webcomics
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
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| « Mar | ||||||
| 1 | 2 | 3 | 4 | 5 | ||
| 6 | 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 15 | 16 | 17 | 18 | 19 |
| 20 | 21 | 22 | 23 | 24 | 25 | 26 |
| 27 | 28 | 29 | 30 | 31 | ||
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!
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)
<%= select_tag "some_select", ("<option></option>" + options_from_collection_for_select(@foo, "id", "item")).html_safe %>
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.
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.
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.
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
I’ve decided on my least favorite phrase in the English language:
“Can’t you just…?”
Why is this my least favorite phrase? Because:
- It’s always in conjunction with a task that the questioner does not understand, but assumes should not be difficult at all;
- it is inherently second-guessing the person being asked, which is insulting;
- 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.
Another edition of Sara uses Rails on Windows! *muppety fanfare*
Trying to get mysql2 to run on Windows is a bit of a pain. I used the lovely installer from Railsinstaller.org and then created an app with :
rails new testapp -d mysql
and then bundled.
By default this installs the mysql2 gem. There are some gotchas, and hopefully this will help:
- You have to install mysql first (derp!). Go here and pick either the 32 or 64 bit MSI installer, and use this guide to pick your options during the install (section II). I know, it’s an old guide, but it works fine. And it has pictures!
- You may get this error: “%1 is not a valid Win32 application.” This means you need to go here and get libmysql.dll, and copy it into c:\RailsInstaller\Ruby1.8.7\bin. Get the right version or your migrations will fail when you try to add indexes – the MySQL 5.5.9 64-bit version of libmysql.dll will NOT work! I got this version and it worked just fine: mysql-connector-c-noinstall-6.0.2-win32-vs2005.zip (you don’t need the MSI installer for this one, just grab a zippy file).
- Next error you may get is “rbreadline.rb:4404: uninitialized constant RbReadline::Encoding (NameError)”. Thanks to this blog post I found the answer is to go into c:\RailsInstaller\Ruby1.8.7\lib\ruby\site_ruby\1.8\rbreadline.rb, and comment out line 4404.
There you go! Rake away!
Banged my head against this for a while. Starting a new rails project, trying to get rails.js from github, but it’s failing every time:
SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (OpenSSL::SSL::SSLError)
So (after a lot of googling) I opened up gems/jquery-rails-0.2.7/lib/generators/jquery/install/install_generator.rb and added this just after the beginning of the InstallGenerator class (“class InstallGenerator < ::Rails::Generators::Base”) :
require ‘openssl’OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE