March 13th, 2009

Distinct Values

module ActiveRecord #mixin for active record
  module ModelHelper # module name
    def self.included(base) # include this into the base of the class instance
      base.class_eval do # evaluate the below
        (class << self;self;end).class_eval do
          def self.distinct_values(column)
            find(:all, :select => "DISTINCT #{column}").map{|x| x.send(column)} if column_names.include?(column)
          end
        end
  </a>    end
    end
  end
end

I like to setup a file in the /lib directory to hold model_helper.rb. I tend to put functions that I like to mix into all of my models into this file. This keeps things dry and it prevents me from running all over wondering where a certain active record model function is located.

def self.distinct_values(column)
  find(:all, :select => "DISTINCT #{column}").map{|x| x.send(column)} if column_names.include?(column)
end

After I mix this function in to all the active records objects I’m give the ability to call it like this:

distinct_items = Model.distinct_values("column_name")
#> ["value1","value2","value3"]

This will return distinct values of a model, instead of the objects themselves. I find this useful in projects where there is sometimes an intentional lack of normalization on a table.

table: posts
+-----+----------+
| id  | category |
+-----+----------+
|  5  | ruby     |
|  35 | ruby     |
|  52 | news     |
|  53 | news     |
|  63 | news     |

so now we can just call.

  Post.distinct_values("category")
  #-> ["ruby","news"]

This is useful for filling dropdown lists, select boxes, etc.

March 2nd, 2009

Reflection

module ActiveRecord
  module ModelHelper
    def self.included(base)
      base.class_eval do
        # This creates class methods that allow me to delve into my associations
        # I can call stuff like reflect_on_has_many and it will return Class names of my has_many associations
   # I can then use them to do whatever i like.
 
        (class << self;self;end).class_eval do
 
          [:has_and_belongs_to_many, :has_one, :has_many].each do |association_sym|
              define_method "reflect_on_#{association_sym.to_s}".to_sym do
                results = self.reflect_on_all_associations.collect do |association|
                  if association.macro == association_sym
                    Class.const_get(association.class_name)
                  end
                 end
                return results.compact
              end
          end
        end
      end
    end
  end
end
March 1st, 2009

Starting Back.

Alright, like the several posts before this, I’m going to say that I will be starting back blogging. Why not, I’ve got a lot to share, but lack the overall skill to share it, or at least that’s my opinion.

I’ll starting work on a side project with a friend of mine travis robertson . I think its a great idea, but I’m going to keep from sharing to much detail until we have a alpha/beta release.

Obviously, I’ve been working full time on Ruby on Rails projects since 2006, and working with ruby since 2005. I’m going to be working on this project with the hope that I’ll create something that I would use myself, and I think other’s will use it as well.

Now as suggest by my good friend Charles here is some Simple Ruby Code:

I’m sure you know those really wordy for loops in C-based languages in this case javascript.

1
2
3
4
5
6
7
8
9
10
var i=0;
for (i=0;<=10;i++)
 
{
 
document.write("The number is " + i);
 
document.write("");
 
}

ruby has a simple alternative.

1
11.times { |i| p "The number is #{i}\n" }

No that’s obviously simple, but there can be some confusion about what count i starts on. In the case of the .times method its starts on 0. However, there are other loops that you can have more control.

In ruby there is a trick with using arrays for loops.

1
[0,1,2,3,4,5,6,7,8,9,10].each {|i| p "The number is #{1}\n" }

Now obviously that seems a little odd, but will get the same result.
But ruby like everything has a shortcut using Ranges.

1
(0..10).each {|i| p "The number is #{1}\n" }

The number of ..dots matter in the above code. Two dots include the 10 in the loop, while 3 dots.

1
(0...10)

will only print 0-9.

Until next time.

May 18th, 2005

RUBY - The True Language of Love

Hey guys,

Well, I finally Graduated with Honors from the University of North Alabama. I’m glad to see it finally end, but also a little sad about saying goodbye to some of the friends I made there this past year. I’ve has suddenly been left with a lot of time on my hands, so I’ve been practicing RUBY.

RUBY is an awesome programming language that, in my opinion, is a real joy to use. I’ve been playing around with it for the past month or so. It took me all but a weekend to get the hang of the syntax, and now I’ve been learning to do more complex things with it. I like it better than even PYTHON, which was my previously held language champion. There is just something about RUBY that makes it fun to use.

Now that I?ve said that I love it, let me bring it back down to earth a bit about something I don’t much care for. For one thing, it?s not widely supported (yet). Secondly, there are still some bugs on the Windows-side of things. Finally, this one is somewhat related to the first, it?s hard to get it working on an APACHE web server.

Once all these things are figured out, I think it will really take off. Obviously, Windows is the most popular OS, so getting all the developer bugs out of it is a must. Apache is the most popular web server, so the same goes for it.

Sometimes, I wish that a language like RUBY could be the good old compiled kind. So I?d never have to use C++ again, but alas, it is not. I’d also like to see a GUI toolkit that is simple to use or at least works just like the language that uses it. The closest thing I?ve seen is wxRUBY for RUBY, but I still don’t like something about it.

I’m quickly beginning to realize that it?s not the programming language that makes the language fun, its more the tools you use to develop with, that make it fun.

For example, I hate hand typing every little tag in an HTML page, so I use Dreamweaver to speed up my workflow, sure you have to still know HTML to make corrections or minor modifications to the generated code, but it still works really well.

The same holds true with Visual C++,C#,J#,BASIC, ETC. It just makes the language more usable instantly. Keeps me from having to type everything in.

So, now I await a Visual RUBY application. I may be smart enough to do it myself one day. You never know.

You guys take it easy,

Jim