I made a list of books that should most likely be read and at least thought about by potential and new entrepreneurs. It’s here on Amazon.
Let us know if we’re missing a key reading. We’d like to read it and add it to the list.
I made a list of books that should most likely be read and at least thought about by potential and new entrepreneurs. It’s here on Amazon.
Let us know if we’re missing a key reading. We’d like to read it and add it to the list.
While going back through an improving some initialization code of our application I moved all require statements for our rails gems into the Rails::Initializer block. This worked great for everyone except one of my coworkers who reported the following error:
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/version.rb:237:in `initialize': undefined method `collect' for #<Gem::Version::Requirement:0x371b2d8> (NoMethodError)
The solution turned out to be very simple. He just needed to update his rubygems installation (gem update --system) and everything went back to working fine.
We’re big fans of javascript. Used correctly it can create a great user experience.
It gives me great pleasure to announce that we at Inigral have decided to develop and share our own Javascript library with the rest of the Facebook development community.
It’s available now at: http://github.com/gdeglin/inigral-fbjs-library
The code for has_many :through polymorphic is pretty straightforward but what if you are using Single Table Inheritance (STI) on either or both sides of the polymorphic through table?
There were a number of painful hacks to make this work until a patch to ActiveRecord by Trevor Squires added the :source_type key to has_many :through polymorphic (hmtp). Check it out here: [PATCH] allow polymorphic :source for has_many :through
The main purpose of this blog post is to write up an example of using :source_type in the hope that people won’t get tripped up by out-of-date blog posts which delt with hmtp before the :source_type patch.
Your web application has Users and Organizations and users belong to an organization through Memberships.
This is a typical example of where you might use has_many :through. But what if you have many subclasses of users - like Student and Admin - and/or you have subclasses of Organizations - like StudentOrganization and AthleticTeam. To make this work you’ll need the :source_type key. Once you have that it’s quite simple:
class User < ActiveRecord::Base
has_many :memberships
has_many :organizations, :through => :memberships, :source => :member_of, :source_type => 'Organization'
end
class Membership < ActiveRecord::Base
belongs_to :user
belongs_to :member_of, :polymorphic => true
end
class Organization < ActiveRecord::Base
has_many :memberships, :as => :member_of
has_many :users, :through => :memberships
end
class Student < User
end
class Admin < User
end
class StudentOrganization < Organization
end
class AthleticTeam < Organization
end
An interesting undocumented piece of ActiveRecord I bumped into today is “count_all”. You can use this inside ActiveRecord count calls to do sorts and limits based on the count when you’re doing GROUP BY.
I came across this because I was having a bit of trouble recently finding the Rails way to find the biggest student group, and when the most recent joining of that group happened. What I wanted was a Railsy way to run this query:
SELECT member_of_id, count(*) mems, max(created_at) most_recent FROM memberships WHERE member_of_type = 'Organization' GROUP BY member_of_id ORDER BY mems DESC LIMIT 1
What this SQL does is return the largest group, the number of members it has, and the time the most recent one was created. It’s pretty fast, too. Now to make it Rails!
ActiveRecord provides us with the powerful count method, which will get us a good portion of the way there, using the group keyword:
Membership.count(:group => "member_of_id", :conditions => "member_of_type = 'Organization'")
This is more Railsy, but it returns a lot of data, and we will need to sort it in memory, and discard the bulk of what we pulled from the database, so it’s a good deal slower. What’s we’d like to do is sort it in the database, and only return the top row. It turns out you can do this:
largest_organization = Membership.count(:group => "member_of_id", :conditions => "member_of_type = 'Organization'", :order => "count_all DESC", :limit => 1)
largest_organization_id = largest_organization.first[0] unless largest_organization.blank?
This may not be perfectly Rails Way, since I’m not sure how legit it is to use count_all in the order by clause. It’s also not quite as fast as the original SQL, because we still need to run another query to get the most recent join time for the organization:
Membership.maximum(:created_at, :conditions => ["member_of_id = ?", largest_organization_id])
Still, it’s pretty good, and stays inside the standard ActiveRecord paradigm.
Enjoy!
You have a laptop, a sever and a facebook app.
Ideally, Facebook would talk to your development machine - but what if you don’t have a static ip address and you can’t use dynamic dns or port forwarding?
Reverse SSH Tunneling is what you need my friend.
The following is a step by step tutorial of what you need to do to have ports on your server forwarded to your local machine via ssh.
Add “GatewayPorts yes” into your sshd_config file:
sudo nano /etc/ssh/sshd_config
– add or edit the GatewayPorts variable, control-O to save, control-X to exit
Reload ssh:
sudo /etc/init.d/ssh reload
Run the tunnel:
ssh -nNT -g -R 3000:0.0.0.0:3000 me@myserver.com
If you want port 1 on your server to go to port 2 on your local it will look like:
ssh -nNT -g -R 1:0.0.0.0:2 you@yourserver.com so you can forward any port on your server to the standard -p 3000 that your rails server is running on.
Add an alias to your ~/bash_profile:
alias sshtunnel="ssh -nNT -g -R 3000:0.0.0.0:3000 library@jorgeluisgorgeous.com"
Add your public key to the server for password free login
Thanks to Paul McKeller for getting me started on this