Recent Posts by Brian Eng

Subscribe to Recent Posts by Brian Eng 49 posts found

Pages: 1 2

Apr 25, 2008
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / nice, dry, validator?

I haven’t tried it myself, but maybe this plugin would work for you.

As for the currency on the fly text input, there are quite a few pure Javascript solutions for this out there… this one looks like it works pretty well. Convert it into a Rails plugin and give it back to us!

Good luck.

 
Feb 6, 2008
Avatar Brian Eng 49 posts

Topic: Rails on Windows / use Command Prompt?

When I was developing on Windows (on OS X now) I used the ol’ Command Prompt. An invaluable technique on *nix-based systems like OS X is tail’ing your log file, instead of looking at your script/server output. On Windows, there are a few ways to do this, including tail.exe from the Windows Server 2003 Resource Kit (it definitely works on XP as well, not sure about Vista). I must admit though, when I was on Windows, I just popped it open in Notepad :)

 
Jan 30, 2008
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Create MySQL columns on the fly

Seems like you’re doing some pretty funky stuff… I won’t ask why :)

Create a method that checks for the existence of the column first.

def column_exists?(table, column)
  column_check_sql = <<-SQL
    SELECT  column_name
    FROM    information_schema.columns
    WHERE   table_name = '#{table}'
    AND     table_schema = '#{ActiveRecord::Base.connection.current_database}'
    AND     column_name = '#{column}'
  SQL

  return ActiveRecord::Base.connection.execute(column_check_sql).num_rows == 1
end

Then, do your column creation code unless column_exists?

HTH… good luck.

 
Jan 29, 2008
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Serving Images via Rails, limiting access via direct link

Have a look at the send_data method. Using send_data, you can put your image files in a non-publicly accessible directory and serve them via a controller action that is authenticated.

 
Nov 7, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / ASP's Response.Write for ERb

You could do something like

<% for animal in barn; concat(animal.name, binding); end %>

But not recommended. What you’ve got in your second example is the preferred approach. Kinda nice actually. There’s a clear distinction between evaluated Ruby code and what you’re going to output to the view.

 
Oct 9, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Two Models of Composing Actions

It should be mentioned that restful_authentication is now somewhat preferred by most in the REST camp vs. AAA… looking at the code there may help clear up some confusion.

On to your questions.

Models != Resources. Models map to database tables. Resources are the… well, resources that your application provides to the outside world via HTTP. If you think of it that way, then the answer to your second question becomes clear. Login and logout (and it is implemented this way in restful_authentication) are simply the create and destroy methods on a “sessions” resource. Note that there is no session model, only a resource. Which is a perfect illustration of how resources do not necessarily have to map one-to-one to something in your database. As for stuff like change_password and forgot_password, it’s up to you whether you’d like to implement those RESTfully or decide that they’re just “oddball” actions that you want to stick on the end of some other controller. Example, you could do something like a ForgottenPasswordController and implement a new/create on that controller, or simply tack it onto the UsersController and be done with it.

Hope this helps.

 
Oct 4, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Working with arrays of hashes

Ed, I think what you’re looking for is the “select” method on the array.

 x.select { |case_study| case_study[:market] == Market::EDUCATION }
 
Aug 31, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / associations and join tables

This is a perfect solution… I’ll just add that there’s one other way to do it (also mentioned in Jamis’ article), and that is to use association extensions.

class Country < ActiveRecord::Base
    has_many :carrier_countries
    has_many :carriers, :through => :carrier_countries do
      def major
        find(:all, :conditions => "major = 1")
      end
    end
end

I sometimes prefer this syntax if you’re going to have more than one type of filter. For example, if you wanted to eventually add carriers that were small, mid-sized, etc… doing it this way, you would then have methods like country.small_carriers, country.major_carriers, etc. With the block syntax it is bit cleaner, as this creates methods like country.carriers.major and country.carriers.small.

 
Aug 31, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Best way to setup this relationship in activerecord

I actually had a typo in the post; sorry about that… I fixed it. Apologies if it confused you. I think you pretty much have a handle on the concept. Basically, “detailable” (which, again, is a horrible word for it… if you can think of something better, use it!) is the “interface” that is implemented by any class that can take on details. So ultimately, your DB might look like:

doctors:
  doctor:
    id: 1
    name: Dr. Dorian

patients:
  patient:
    id: 1
    name:  John Doe

details:
  detail:
    id: 1
    detailable_id: 1
    detailable_type: Doctor
    address: 123 Main St
    city: Chicago
  detail:
    id: 2
    detailable_id: 1
    detailable_type: Patient
    address: 555 West Elm
    city:  New York

So, in this example, both detail records have the ID of 1 but a different type. This is because both the Doctor and Patient classes are able to implement the same interface (or set of attributes).

HTH.

 
Aug 31, 2007
Avatar Brian Eng 49 posts

Topic: Rails on Windows / VibrantInk VS settings

Yeah, John Lam’s theme for VS (the second link) is pretty much what we had there, but a bit more thorough. I’d recommend it.

 
Aug 30, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Being able to search for nearby items based on zip code

Yep, this is indeed a common requirement, and there are a great many ways to implement it. Depends how accurate you need to be.

If you want to order by distance, you certainly need said huge table of zip codes and lat/longs. Just Google it. Then, implement something like a Great-Circle algorithm or something. There’s an implementation of Great-Circle in Ruby if you’re interested.

 
Aug 30, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Best way to setup this relationship in activerecord

You could do STI; it would probably work fine for you in this case. But the OO design guy on my shoulder tells me you should be using polymorphism, which Rails luckily supports quite easily. Seems like you have to classes which are both “detailable”... ok, that’s a horrible word for it, but let’s go with that for now. These are just suggestions—how you choose to implement this is up to you, but I would probably do something like:

Have a separate table and class for the purposes of your common attributes, say “details”.

id, detailable_id, detailable_type, first_name, last_name, title, gender, birthday, etc.

class Detail < ActiveRecord::Base
  belongs_to :detailable, :polymorphic => true
end

class Doctor < ActiveRecord::Base
  has_one :detail, :as => :detailable
end

# Repeat for every class that is "Detailable" 

There’s plenty of documentation out there on using polymorphic associations in Rails… hopefully this gets you started.

 
Aug 23, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / ASP.NET -> Rails Equivalent for Placeholder.Visible?

Good point Mike, I’ve found that technique useful as well. Re: your code formatting—wrap it with pre tag.

 
Aug 22, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / ASP.NET -> Rails Equivalent for Placeholder.Visible?

Did I say application controller? I meant helper. Sorry.

 
Aug 22, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Hiding a column

The paginate method (which is going to be deprecated, if I’m not mistaken) is much like a find, except it returns a two-element array of a Paginator and a collection of the records for the current page. It works by automatically providing a LIMIT and OFFSET to the SQL query that is created.

See here for more: http://api.rubyonrails.org/classes/ActionController/Pagination.html#M000130

 
Aug 22, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / ASP.NET -> Rails Equivalent for Placeholder.Visible?

Refactor the logic down to the controller or helper level.

As a simple example, put a method in your application controller helper:

def show_header
  return "THIS IS THE HEADER" if params[:state] && @cities.total_results_count.to_i > 0
end

Then, in the view/layout, simply:

<%= show_header %>

As a basic rule of thumb, keep your views clean and simple. Refactor the complexity down to helpers, controllers, and models.

Hope this helps.

 
Aug 20, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Mephisto vs. Typo vs ?

Yeah, a lot of Rails blogs (including ours) have recently converted to Mephisto… for good reason. I can’t speak for more recent versions of Typo, but for us, it had a ton of memory issues. It’s also has a lot more features than Mephisto… a lot more than we needed to run Softies on Rails. Mephisto has its shortcomings too, but it has worked well for our needs. For more minimal needs, Radiant is a solid app. But it doesn’t seem to have a lot of the “blogging” features one would expect, especially if you’re used to something like Blogger or Wordpress. It seems to be much more well-suited as a basic CMS for a static site. Hope this helps.

 
Aug 8, 2007
Avatar Brian Eng 49 posts

Topic: Mac Switchers / mounting Linux dirs on a Mac?

Are you simply trying to mount a remote drive? If so, it’s as easy as going to the finder and hitting command-K. Then put in smb://whatever.

 
Jun 21, 2007
Avatar Brian Eng 49 posts

Topic: Mac Switchers / Considering the switch to force myself to learn Ruby

There’s no PC-style “delete” key on the Pro either. It’s labeled “delete” but does what we (former) PC users would call backspace. Have to fn-delete in order to do a “delete”. Yeah, I’m confused too.

 
Jun 20, 2007
Avatar Brian Eng 49 posts

Topic: Mac Switchers / Considering the switch to force myself to learn Ruby

I can’t tell you what the experience will be for you… but for me, switching to Mac was the best decision I’ve ever made. Honestly, It took me about 2 months to get used to the idiosyncrasies of OS X. I think that was less about OS X itself and more about me getting out of the way Windows “taught” me to think and work over the years.

You will miss Visual Studio. At first. Especially Intellisense. But I’ve found that not having Intellisense makes me a better developer. If you’re anything like me, you’ll see what I mean. Really.

I own the Biggest Thing That Could Possibly Work (the 17” Macbook Pro), but the Macbook works fine for most people.

Good luck!

 
Jun 15, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Goin the other direction...

Sorry to hear that :)

You’re going to have a hard time wrapping your mind around ASP.NET if this is your first foray into Microsoft-land. ASP.NET is built very much around a desktop programming model (e.g. you have forms that have controls that raise events, etc). Much like Rails (I can’t believe I just said that), there is a convention that you should follow, and if you choose not to, it’s going to hurt.

The equivalent of a “partial” in ASP.NET (correct me if I’m wrong folks, it’s been a while) would be to use a Web User Control. If you’re using .NET 2.0+, there is the concept of “Master Pages” which are a lot like “layouts” in Rails.

I which I could point you to a single, definitive “getting started with ASP.NET” article or book. I suppose the ASP.NET quickstarts on MSDN are as good a place as any.

Best of luck.

 
May 25, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Noobkit Docs - Ruby on Rails documentation... on Rails :)

Wow, impressive. Seriously good stuff.

 
Apr 23, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Ok lets go down the route of Resfulnesss

Yeah, I have to deal with this type of thing as well. I haven’t come up with a really elegant solution besides the obvious, brute-force method either, i.e.

conditions << “AND av_support = true” if model.av_support?

Sounds like a neat plugin in the making, though :)

 
Apr 11, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / Ok lets go down the route of Resfulnesss

There is quite a bit of debate about the whole “search as REST/CRUD” thing out there. Some argue that search is an operation, not a resource. I think it’s important to keep in mind that RESTful design is an aspiration, not the be-all-end-all. Of course, you will have “one-off” or “oddball” actions muddying the waters from time to time. But the fact that you begin to think RESTfully about everything in your application will buy you a ton.

 
Feb 28, 2007
Avatar Brian Eng 49 posts

Topic: General Ruby/Rails Discussion / I am annoyed with Capistrano tonight

Glad you got it figured out.

One thing though, I’m pretty sure that deploy.rb isn’t used on the server-side at all. It is only used on the client-side to determine what command to send through SSH. (Try not adding deploy.rb to your svn repository at all, and see if it still works.)

Next page

Pages: 1 2