Rails: Hack belongs_to to allow :select

[ Posted by kevin Tue, 17 Apr 2007 22:57:52 GMT ]

For some reason most rails associations allow :select as a parameter to limit which rows are loaded, but belongs_to doesn't.

Here's my (only rudimentarily tested) monkey-patch to add it:

module ActiveRecord::Associations::ClassMethods
  def create_belongs_to_reflection(association_id, options)
    $stderr.puts "MAKING REFLECTION"
    options.assert_valid_keys(
      :select, #added
      :class_name, :foreign_key, :foreign_type, :remote, :conditions, :order, :include, :dependent, 
      :counter_cache, :extend, :polymorphic
    )
    
    reflection = create_reflection(:belongs_to, association_id, options, self)

    if options[:polymorphic]
      reflection.options[:foreign_type] ||= reflection.class_name.underscore + "_type"
    end

    reflection
  end
end
module ActiveRecord
  module Associations
    class BelongsToAssociation  @reflection.options[:select] || '*', #added
          :conditions => conditions,
          :include    => @reflection.options[:include]
        )
      end
    end
  end
end

Technorati Tags: ,

Posted in  | 9 comments

Rails: How to eager load associations with :select (only load select columns from each table)

[ Posted by kevin Fri, 13 Apr 2007 20:51:29 GMT ]

I've been avoiding blogging about things that were overly technical for awhile, but screw it, I need more things to write about anyways, so I'll start sharing nuggets of knowledge here as well.

Rails eager loading lets you load sub-elements on the database level, which can be a huge speed increase, though sometimes at a database cost. In my experience, the biggest problem is that items I'm joining together often have a big 'description' column, that can cause memory issues if loaded too indescriminatingly.

@logs = ItemLog.find(:all, :conditions => "target_id=#{@user.id}", :order => 'item_logs.created_at DESC', :limit => 40, :include => 'item')

What I wanted to do was add :select => 'only_rows_i_want'. But rails doesn't let you use :select with :include.

So what I did was do it without the :select, and take a look at the log file to see what the generated query would be. Then I removed the items I wanted and used it as a find_by_sql query:

rows = ItemLog.find_by_sql("SELECT item_logs.`id` AS t0_r0, item_logs.`user_id` AS t0_r1, item_logs.`item_id` AS t0_r2, item_logs.`created_at` AS t0_r3, item_logs.`referer` AS t0_r4, item_logs.`target_id` AS t0_r5, items.`id` AS t1_r0, items.`user_id` AS t1_r1, items.`title` AS t1_r4 FROM item_logs LEFT OUTER JOIN items ON items.id = item_logs.item_id WHERE (target_id=#{@user.id}) ORDER BY item_logs.created_at DESC LIMIT 50")

Then how do you get it to make the sub-elements into the actual objects?

join_dep = ActiveRecord::Associations::ClassMethods::JoinDependency.new(ItemLog, ['item'], nil)
@logs = join_dep.instantiate(rows)

I've put the name of the class I'm doing the find on as the first parameter, the include field I used as the 2nd. It works great with rails 1.2!

Technorati Tags: , ,

Posted in  | 5 comments

Review: A heartbreaking work of staggering genius

[ Posted by kevin Sun, 14 Jan 2007 21:16:40 GMT ]


"A Heartbreaking Work of Staggering Genius" (Dave Eggers)

This is an autobiography. I wish someone would have told me that, because I hate autobiographies. I'll go as far as to say that no-one's life is that interesting... everything needs a plot.

That said, this guy has a fascinating writing style. For a really dull book, it had some interesting moments. Many of what he says is very poetic, and rang with that quiet bell of truth that you can recognize sometimes. His paranoia and genuine love of his brother and friends, while at the same time desire to use them in his book (yes, it's meta like that), was endearing in a quirky sort of way.

It was very different from my usual fantasy-fare, so was a refreshing read.

Technorati Tags: ,

Posted in  | 4 comments

What should we replace HTML with?

[ Posted by kevin Mon, 08 Jan 2007 21:20:50 GMT ]

I was thinking about this relative to my old post about designing web pages for different screen sizes.

I shall blog about this another day (if I'm going to share it with you instead of capitalizing on it by keeping it secret, but wait, then no-one would adopt it and it would fail. Perhaps that's then the real success of open source?)

In related news, apple recently got a patent for designing UI's based on screen-independent width values. Like all patents, seems kinda obvious, but here we are not doing it.

Technorati Tags: , , ,

Posted in  | 2 comments

It's crazy... that cats are $free

[ Posted by kevin Mon, 08 Jan 2007 21:15:36 GMT ]

Doesn't a monkey sound exotic? Surely it would cost several thousand dollars, not to mention being inhumane to own one anyways.

But you can get these fantastic creatures called 'cats' for free. In fact, every major city has several houses set up to house the ones they can't give away fast enough. Crazy!

I'd pay a lot for such an exotic pet. They're so ... feline. So inhuman. It's really fascinating, how much it teaches us how animal humans are by what we share vs. don't share.

Technorati Tags: ,

Posted in  | 3 comments

Quotes from a (bad) rambling book: A heartbreaking work of Staggering Genius

[ Posted by kevin Sun, 07 Jan 2007 23:39:00 GMT ]

When I was in high school and an undergrad in college, I would find a passage in a book that really spoke to me in a special way. Something that I swore was written just for me. I'd read it to someone, and they'd say 'heh', and not be that interested. I dare you:

"This is obscene. How dare we be standing around,t alking about nothing, not running in one huge mass of people, running at something, something huge, knocking it over? Why do we all bother coming out, gathering her in numbers like this, without starting fires, tearing things down? How dare we not lock the doors and replace the white bulbs with red and commence with the massive orgy, the joyous mingling of a thousand arms, legs, breasts" - page 134

I love the poetic-ness, the feeling that there must be something bigger, some grander purpose to socialization. That feeling you get when the conversation is just a bit too dull, and you're like "why am I here?" When you're looking at the girls and wishing you had the nerve to say something, the wit to think of something clever that would make them love you, that would make the whole room recognize your genius. OK, well maybe a bit less ego-centric than that, but one giant shared-love experience, that we're all ok. We all crave that attention, that acceptance, isn't that what an orgy represents anyways? One giant community fulfillment?

In case you couldn't tell, I've been thinking a lot lately about what makes a community, and why we bother finding or joining one. I'm not sure that I ever really have - certainly friends, and groups of friends, but never neighbors in a very real way, or any other physical or ideological grouping. I yearn for that grander purpose to start fires and tear things down - I had a taste of it in the WTO protests in Seattle 1999.

Yes, it's been awhile. Welcome back, Kevin.

Technorati Tags: , , ,

Posted in ,  | 3 comments

Ebay claims "operating cost per listing" as reason behind price increase

[ Posted by kevin Wed, 16 Aug 2006 20:40:49 GMT ]

Ebay has always pissed me off, even though I use them quite a lot lately. They basically have no competition, and are the catch-all advertising for selling anything on the internet. It’s a stupid system that needs to change, but how do you redirect all those searchers that have “ebay” imprinted on their forehead as the only place to shop online besides amazon?

They also just charge too damn much. 6% on large listings, plus $6 for each item you post (whether or not it sells).

From
http://pages.ebay.com/sell/announcement200607/overview/index.html

And, when you compare our operations costs for an average Store Inventory listing and an average core listing – factoring in the duration of each – our cost to host a Store Inventory listing is more than 50% higher than for a core listing. In fact, current Store Inventory insertion fees don’t cover eBay’s costs for hosting them.


Bullox! It’s run by a bloody computer, buy a few more servers and show us the revolution of computers. You should be able to host kazillions of auctions with no noticeable speed decrease. Google shows us how easy it is to scale… This is such a “I don’t understand how computers work and I’m a marketing person” response…

Technorati Tags:

Posted in ,  | 2 comments

Why the Island is more profound/better than the Matrix

[ Posted by kevin Tue, 15 Aug 2006 14:08:27 GMT ]

The matrix was one of the most popular movies of all time, while the Island didn’t even get very good reviews. They share the similarity of a “virtual world” that people awaken from, which may have caused the bad reviews. Truly, though, people have been making movies and books about awakening since the beginning of time.

Why is the Island so much better than the Matrix?

1) It’s more realistic.

Neo in the Matrix transcended both virtual and actual realities, and that distances the viewers from identifying with the film. While the Matrix is a future world that might happen if we start battling with sentient computers, the Island is just one short medical breakthrough away. The society of the “real world” presented in the Island is very similar to our own.

2) The bad guys are badder.

The matrix’s computers were content to let us live out our little lives in a virtual existence. They even went to a lot of trouble to make sure we were happy in our environment (in that utopia didn’t work).

The Island is the only thing I’ve seen that justifies the horror of the death camps of World War 2. The incinerator scene still echoes in my head.

3) It’s easier to identify with the Island, because I’m not an evil computer program trying to use people as batteries. But if I could pay money to guarantee an organ transplant, I would. I can see myself as both product and sponsor, and the only person I struggle to identify with is the evil doctor leading the organization. If they’d thrown in just a little bit more medical excitement about the dreams-of-sponsor somehow being in the transfer, and that being intentional research and benefiting the world, then “justifying” the cruelty would have been a lot easier.

To me, the big paradigm of the Island is the justification of the horror. The only horror equal to it was the death camps of World War 2, and I (and most people) have never understood their justification or how they ever happened. It’s been nearly a month since I first watched the Island, and I still think of it often or dream about it. If you haven’t seen it yet, I highly recommend it.

Technorati Tags: , , ,

Posted in  | 1 comment

Street Sweeping is a Disservice

[ Posted by kevin Wed, 26 Jul 2006 15:03:15 GMT ]

The street isn’t even dirty! These bastards come through once every two weeks on prime-numbered days and expect us to remember.

They could at least just honk and let me know to move my car instead of giving me a ticket each time.

Who the hell builds houses without driveways anyways? Didn’t they see this coming? :)

Seriously though, I worry that “services” like these that end up being a big fundraiser for the city at the expense of it’s citizens are a broken idea.

Technorati Tags: ,

Posted in  | 4 comments

Life boring? You just need a producer!

[ Posted by kevin Thu, 22 Jun 2006 13:43:00 GMT ]

Think of this way… what if “American Idol” had been produced solely by the capabilities of the contestants themselves, without the expertise and talent of the show’s producers, directors, writers, etc. As talented and entertaining as the contestants are, the resulting production quality, the level of emotional engagement, viewership/ratings and monetization potential of the full package would likely be far inferior to what we all see on the air today. Well, social networks should be seen in a similar way… people want to express themselves and the platforms that allow them to do so with the most creativity and production value, are the ones that people will flock to.”

from over here at http://gigaom.com/2006/05/29/social-networks-are-the-new-media/

I’d love to see an “interesting” makeover (God, I just know they must already have a TV show about this somewhere, I just don’t have a TV so I’m out of the loop).

I think you can teach people a lot about being interesting.

1) Conversation – How to pay attention to whether people are still listening. How to make things a bit more grandiose without going over the top. Friendly flirting.
2) Your routine & breaking out of it!
3) The importance of inspiration, and inspiration’s dependency on creativity.
4) Spunk exercises – how to get excited about life. You’ve got the energy somewhere inside of you!

Sign up for our exclusive class and discover the ‘you’, you’ve always wanted to know. Brainwashing and after-therapy not included. And yes, I’m serious I think people could use more excitingness, but no there’s no class – sorry! :)

Technorati Tags: ,

Posted in  | 2 comments

Older posts: 1 2 3 ... 8