April 2, 2007

Rails: flatten your content model with single-table inheritance and alias_attribute

I’m currently working on a Ruby on Rails app with a very deep content model. There are many different “same-but-different” types of content to be managed. The “same” parts are roughly similar structure, like simple list items. The “different” parts are usually more meta-data oriented, such as different field labels.

I didn’t want to end up with dozens of tables in the db for all these types. I also liked the idea of being able to easily search across them content types, so a relatively flat db structure would facilitate that.

My solution? Create a single table with some generic content holding fields something like the following:

#migration
class CreateListItems < ActiveRecord::Migration
  def self.up
    create_table :list_items do |t|
      t.column :owner_id, :integer
      t.column :type, :string
      t.column :text, :text
      t.column :text2, :text
    end
  end

  def self.down
    drop_table :list_items
  end
end

# model
class ListItem < ActiveRecord::Base
  validates_presence_of :text
end

Now, for the various content types, use single-table inheritance to let all the models track back to the same table (the “type” field above is necessary for this to work). Also, to make the attributes match your content types, use an alias_attribute call to map the attribute names to the generic ones in the parent table. Such as:

class Question < ListItem

  alias_attribute :question, :text
  alias_attribute :answer, :text2

end

Now you’ll be able interact with the Question instances naturally (q.question=, q.answer=), and any other content types you derive from ListItem.

Cat: 
  • Ruby on Rails
 | Time: 9:34 pm (UTC-6)  Comments (0)

Dynamics AX: Making .NET calls from inside AX

In a typical fashion, Microsoft spent a lot of effort integrating a variety of it’s new technologies in Dynamic AX 4 (nee Axapta). Often these additions are of questionable benefit to the end-user/customer — but, the ability to make direct calls to the .NET/CLR from AX’s built in X++ language is pretty handy for the developer.

The process is pretty simple. Add a reference in the AOT to your CLR assembly — or a reference to .NET’s “System” namespace is already available. Assert permissions to access the bridge (just a wrapper for Window auth), then make your calls.

You declare your variables in their native CLR types, and use the methods of the CLRInterop class in AX to caste to AX data types. Here’s an example to build an AX container of file names in a directory, something trivial in .NET that’s actually a bit of a pain with AX’s limited built in file/directory methods.

void buildFileList( str _folder )
{
    InteropPermission perm = new InteropPermission( InteropKind::ClrInterop );
    container fList;
    System.Array dirList;
    int ix;
    ;

    perm.assert();

    dirList = System.IO.Directory::GetFiles( _folder );
    for( ix=0; ix < ClrInterop::getAnyTypeForObject( dirList.get_Length() ); ix++ )
    {
        fList = conins( fList, conlen(fList)+1, ClrInterop::getAnyTypeForObject( dirList.GetValue(ix) ) );
    }
    return fList;
}

Cat: 
  • Dynamics AX
  • .NET
 | Time: 6:35 am (UTC-6)  Comments (0)

March 26, 2007

Slicehost: installing imagemagick

Get imagemagick running with RMagick on your Ubuntu slice:

sudo apt-get install imagemagick
sudo apt-get install imagemagick9-dev
sudo gem install rmagick

Cat: 
  • Slicehost
 | Time: 7:23 am (UTC-6)  Comments (2)

March 14, 2007

Rails: model == file

If you’re thinking about saving yourself a few files by placing a couple of model classes in the same file in your Rails app, think again. It seems that Rails load structure (at least in recent versions) will puke on that setup.

I was using Single Table Inheritance, and it seems a good idea — instead I spent a hour troubleshooting an “uninitialized constant” error. Once I split the classes into their own model files I was fine.

Cat: 
  • Ruby on Rails
 | Time: 9:10 pm (UTC-6)  Comments (0)

March 13, 2007

RFC: Reading material

Ok, I’m bored. About 6 months ago I did a major RSS feed purge. I had a few too many time wasters in the list, and I migrated to Google Reader from NetNewWire at the same time…but, now, I’m bored. I don’t want junk, but I need a few more good tech feeds.

So I’m appealing to my tech buds to point me to a few good feeds. Looking for high signal-to-noise ratios, only, please.

Cat: 
  • Misc
 | Time: 9:02 pm (UTC-6)  Comments (1)

March 4, 2007

sIFR

Cool Flash text replacement technique. Very interesting.

Cat: 
  • HTML
 | Time: 10:08 pm (UTC-6)  Comments (0)

March 2, 2007

Free acoustic guitar ringtones

I’ve intended to do this for a while, but tonight I actual sat down for a few minutes and did it. Here for your listening pleasure, are a couple of quickie acoustic guitar ringtones. I gated them down and mixed them for looping. They probably don’t sound spectacular on a good speaker, but they sound pretty fine on my Treo. They are plain MP3 files. If you know how to load MP3 files on your phone you can probably use these as ringtones.

Personally, on my Treo, I use Ringo to assign tones to different users, and load the MP3s on an SD card with a card reader. Anyway, here they are. I think I may do a few more of these before I’m done.

Ringtone1

Preview:

Download

Ringtone2

Preview:

Download

Cat: 
  • Ringtones
 | Time: 9:16 pm (UTC-6)  Comments (1)

March 1, 2007

Rails: content_tag block fix

Currently (as of 1.2.2), Ruby on Rails useful content_tag method fails if you try to pass a block to it in code. The following syntax works in a view template:

<% content_for('div') do %>
this is the <b>div content</b>.
<% end %>

But, if you try to do the equivalent in ruby code (like in a helper), such as the below, it tanks:

content_for('div') do
   'this is the <b>div content</b>'
end

While I’m far from an expert on Ruby details, I did a little re-write of the method. If you drop the following in your application_helper (or other place of your choice) you’ll get a fully functional version:

  def content_tag(name, content_or_options_with_block = nil, options = nil, &block)
    if block_given?
      options = content_or_options_with_block if content_or_options_with_block.is_a?(Hash)
      content = block.call
    else
      content = content_or_options_with_block
    end
    content_tag_string(name, content, options)
  end

Cat: 
  • Ruby on Rails
 | Time: 9:44 pm (UTC-6)  Comments (4)

February 27, 2007

Dallas.rb, now with Dave Thomas!

The March Dallas.rb meeting will feature the Dave Thomas speaking on Metaprogramming in Ruby. Dave literally wrote the book on Ruby, and also Rails. He’s also an excellent speaker.

The Dallas.rb crew has worked out special digs for this meeting, with refreshments, etc, so if you’re in the DFW area and have the slightess interest in programming, you would probably find this a worthwhile evening…so come on down.

Cat: 
  • Ruby on Rails
  • Dallas.rb
 | Time: 10:00 pm (UTC-6)  Comments (0)

February 26, 2007

Big time

It was a difficult decision to leave my job of 11 years last Fall. I had wanted to for a long time, and was confident that I’d do well on my own — but, still, I had all the usual fears as the primary breadwinner and all. Luckily, I can report everything’s going great.

It’s a bit more hectic at times, but I’ve got a great relationship with Clients First doing Dynamic AX/.NET development part time that provides me a solid base pay and benefits. I continue to have a good client in my former employer and plan to go-live with a project management application for them in about a month.

And now, after taking a Rails gig for Rice University last week and today accepting a large project developing a Servoy solution for a Manufacturing company nearby, I’ll be fully (or maybe slightly over) booked for the foreseeable future, all with clients that I can expect to continue to do work for supporting and enhancing solutions for years.

I have a lot of people to thank for being where I am. These jobs have come to me through relationships I’ve developed over the years with people I’ve worked with, and not through any purposeful marketing on my part. It’s flattering to know they have that level of confidence in me and my abilities. And, I assure you, I’m not just saying that to suck up so no one gets mad at me if my productivity falls off after the new baby arrives in May. spacer

Cat: 
  • Work
  • Family and Friends
 | Time: 9:57 pm (UTC-6)  Comments (1)

February 25, 2007

Django vs. Rails

I posted some comments RE: Django over at Jim’s blog. I was planning to post some comments here, but the timing of his post prompted me.

Cat: 
  • Ruby on Rails
  • Python
  • PyCon
 | Time: 9:43 pm (UTC-6)  Comments (0)

February 23, 2007

PyCon: One laptop per child

The morning keynote just finished. Ivan Krstic from the One Laptop Per Child project presented a lot of interesting information about the project, and even gave away 2 of their prototype machines. Most pertinent to his appearance at PyCon was the fact that they are implementing all their software in Python — even the file system. And there’s actually a “view source” key on the keyboard to allow a child to flip from the applications to their source code instantly. Very cool.

Lots of neat hardware going into these things, too. They dropped the charging crank for durability concerns, but have a USB port with a smart DC in that can charge from “almost anything” — from another battery to a solar panel. LED backlit display that’s viewable in sunlight. Wireless that can carry laptop to laptop for about 2 kilometers.

Cat: 
  • Python
  • PyCon
 | Time: 10:37 am (UTC-6)  Comments (0)

February 22, 2007

PyCon: Django

I’m currently in the full day of Django tutorials at PyCon. Hope no one catches on that I’m also pecking away at a Rails app while listening! spacer

Cat: 
  • Python
  • PyCon
 | Time: 9:37 am (UTC-6)  Comments (0)

February 20, 2007

New Rails project

Well, after using it for personal applications, skunk works projects that I haven’t had time to actually launch, and my consulting site — I just got the go ahead on my first professional Ruby on Rails project!

I’m really excited about it because it’s also a first co-venture with Jamie, a good friend and awesome designer. More details will emerge in the coming months.

Cat: 
  • Ruby on Rails
 | Time: 4:28 pm (UTC-6)  Comments (0)

February 19, 2007

More eBay’ing

Clearing out a few more records and old cellphones this week on eBay. It’ll be fun to see how the punk records do, it’s been spotty some are appearently pretty collectable based on past experience.

Cat: 
  • eBay
 | Time: 9:56 pm (UTC-6)  Comments (0)

Next Page »

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.