Topic: another sorting issue/question

Hi,

Ok.. so I am just wanting to improve a minor detail of this application.  In the contacts_controller.rb file, there is a line:

order = 'last_name,first_name,company'

The problem is, some contacts don't have last names.. some don't have first names (only emails).

So the result is something like this:

gerry@domain.com
Amy
Brett
Zack
Anne Davis
Carla Hicks
Josh Carr

So, obviously what I need is some kind of way to do this-- but I'm not sure if this is the way to do it, or if this is even valid syntax since I am not a ruby master.

if params[:last_name]==nil
params[:last_name]=params[:first_name]
fakename=true
else if params[:last_name]==nil and params[:first_name]==nil and params[:company]==nil
params[:last_name]=params[:email]
fakename=true
else if params[:last_name]==nil and params[:first_name]==nil
params[:last_name]=params[:company]
fakename=true
end
order = 'last_name,first_name,company'
if fakename==true
params[:last_name]=nil
end

If it's not clear-- my thought was, if a last name is not present, then add the first name to the last name field so that it will sort properly...  And if the first or last name is not present, then set the email to the last name.  But temporarily so that when it actually prints out the first and last names in the view file, it won't print "judy judy" for example...  Anyway, just curious what you ruby experts would advise for this.

the controller .rb file is here
and the view .rhtml file is here

Thank you for your time.

-patrick

Re: another sorting issue/question

It looks like you are doing pagination. Only the records for the given page will be returned, so that's all Ruby has access to. In other words, you will have to do this sorting logic using SQL. Unfortunately, conditional logic like this is a pain to do in SQL, but it is possible (to some extent anyway). What database engine are you using?

Railscasts - Free Ruby on Rails Screencasts

Re: another sorting issue/question

The database is postgresql.

Hmm.. Speaking of pagination, that was another enhancement I was interested in doing..  right now  the contacts list is about 141 pages (over 7000 contacts), but to me-- that is so dumb, because pagination isn't even useful on that level.  I was thinking, it would be much more useful to break the pages up by alphabet letter, and have A-Z...

Is that a simple thing to do?

this is the _pagination.rhtml file that is in the view directory:

<div class="element_links">
        <%= link_to "&lt; Prev", { :page => @contacts_paged.current.previous, :id => @search, :first_name => @first_name, :last_name => @last_name } if @contacts_paged.current.previous %>
        &nbsp;
        <%= pagination_links(@contacts_paged, :window_size => 20, :params => { :id => @search, :newsletter_id => @newsletter_id, :first_name => @first_name, :last_name => @last_name } ) %>
        &nbsp;
        <%= link_to "Next &gt;", { :page => @contacts_paged.current.next, :id => @search, :first_name => @first_name, :last_name => @last_name } if @contacts_paged.current.next %>
</div>

-patrick

Re: another sorting issue/question

You'll want to look into conditional expressions if you want to do this sorting through PostgreSQL. But, if you don't want pagination, you may be able to get by with sorting in Ruby.

An alphabet index can be kind of tricky, but you seem to catch on quickly.

For the links, you will need to loop through every letter of the alphabet. Thankfully Ruby ranges makes this pretty easy:

<div class="element_links">
<% for char in 'A'..'Z' %>
  <%= link_to char, :char => char, :id => @search, :first_name => @first_name, :last_name => @last_name %>
<% end %>

Now comes the hard part, you want the clicked char to be the first character for that contact. But as you said not every contact has a name. Is this a large number? Because you may want to make a special "no name" section along with each letter. The search for last name might look something like this:

@contacts = Contact.find(:all, :conditions => ["last_name LIKE ?", char + '%'])

But, it looks like the search that is being performed is quite a bit more complicated with plenty of other conditions. There's also a comment saying the LIKE '..%' syntax doesn't work. You could try to add it like this:

unless params[:char].blank?
  sql_string << "LOWER(last_name) ~* ?"
  conditions << '^' + params[:char]
end

# that goes above this line in contacts_controller.rb
sql_string = search_sql.join(' AND ')


I have no clue how well that will work.

Railscasts - Free Ruby on Rails Screencasts

Re: another sorting issue/question

Thanks for the reply...

There aren't too many without a name..  There are a handful that are only in the database by email address, and a handful that are 'company names', and then several who are first name only.. and then the rest (majority) have first & last names.

I was thinking, ideally, I would have something like this
"[first name] [last name]  A B C D E F ..etc.."  So that you could click if you are sorting by first or last name.

Well, it seems like this is a bit of work, and so I may hold off on doing this until I have a chance to read some ruby books and get more of a grasp on the syntax.  I'm a 6502 / applesoft, gfa, applescript, and (newly) php person... So Ruby is a little shocking with it's large repertoire of functions.

-Patrick

Last edited by patrick99e99 (2006-12-07 18:42:16)

Re: another sorting issue/question

I think you will like Ruby. It is very useful outside of Rails too. I use it quite a bit for little scripts here and there.

I would say the three top books are:

Programming Ruby
Agile Web Development with Rails
Ruby for Rails

BTW, if you're on a Mac and haven't already, get TextMate. wink

Railscasts - Free Ruby on Rails Screencasts

Re: another sorting issue/question

Oh, I've got textmate..  It's my favorite text editor aside from vi!

Thanks for the recommendations! I will get these from amazon shortly.

-patrick

Re: another sorting issue/question

I am back!  Ok, so over the last few weeks I have been reading the book "Ruby for Rails"..  Which is excellent and very clear.  Thank you for that recommendation.

I wanted to write and ask the important question:  When is it the best time to use Ruby?

I ask this because, I coded and designed the website for this same company that this web application was written for..  And I did everything in PHP...   I did not use a database, but rather created indidivudal php files that held array variables which contained various content which then the webpages sort and display dynamically.

Specifically, take a look at this page:
http://budokon.com/schedule.php?selected=schedule

This page, includes a schedule php file which contains nothing but arrays of data, and then depending on which "icon" is selected, it displays only that data...

The problem I am facing is, I built this website from the most open perspective that I was capable of--  I wanted it to be very easy to expand upon it, and literally wanted it to run itself...  Now I am constantly getting schedule updates from this company, asking that I add various dates, etc..  And I really am seeing how I need to be having this access a database rather than my array file..  And more importantly, I need to build an admin page where they can add their own schedule data.

So obviously, the admin page is something that makes perfect sense to be done with Ruby..

But my html pages as of now, sorting via php..  Does it make sense to have that be a ruby program instead?  would I be better off learning how to access a database via php and set it up that way?

As always, thanks in advace for your time and knowledge.

-patrick

Re: another sorting issue/question

Since you are creating an administration section in Rails, you may find you are already duplicating a lot of the behavior in the current PHP app (showing the schedule list, etc.). If it were me I would replace the PHP completely. This way you can place the administration functionality inline with the rest of the app, so there's no need to go to a completely separate section to do administration. The owners just log-in and see an edit button next to each item in the schedule. This would require some basic user authentication and permission handling though.

If you don't feel ready to replace the full site, just building the administration section first should give you a good enough idea if you are ready to replace the main site with it.

Railscasts - Free Ruby on Rails Screencasts

Re: another sorting issue/question

when you say "full site", are you saying that you would replace ALL php on all web pages with Ruby?

I was thinking, I just wanted to add to the current ruby application, which as I explained before, is just a contact database hub, which allows the company to mass email newsletters, search, and tag their individual customers in the database by area, product, or whatever they wish.  So I was imagining adding a page on that application which allowed them to add schedule data, which would be separate from the actual website...

But you think that's not a good idea?

-p

Re: another sorting issue/question

patrick99e99 wrote:

when you say "full site", are you saying that you would replace ALL php on all web pages with Ruby?

In the whole world? Nope, but that might be nice. wink All pages on the Budokon site? Yep, that's what I meant. smile

patrick99e99 wrote:

I was thinking, I just wanted to add to the current ruby application, which as I explained before, is just a contact database hub, which allows the company to mass email newsletters, search, and tag their individual customers in the database by area, product, or whatever they wish.  So I was imagining adding a page on that application which allowed them to add schedule data, which would be separate from the actual website...

But you think that's not a good idea?

Oh, I didn't realize you already had a rails site built for this client. In that case simply extending the site by adding a controller for administering the schedule seems like a good way to go. It shouldn't be too hard to change the PHP app to access the database instead of the array.

Railscasts - Free Ruby on Rails Screencasts

Re: another sorting issue/question

but it wouldn't be recommended to keep my php, and just modify that particular section to use ruby, or is it impossible to integrate the two?  I am trying to not have to rewrite everything because that site took quite a bit of time the first time around with php...

-p

Re: another sorting issue/question

patrick99e99 wrote:

but it wouldn't be recommended to keep my php, and just modify that particular section to use ruby, or is it impossible to integrate the two?

Do you mean integrate as in use the same database? PHP is very capable of connecting to Rails's database and grabbing the data.

patrick99e99 wrote:

I am trying to not have to rewrite everything because that site took quite a bit of time the first time around with php...

Rewriting the site in Rails is just an option. Since you have invested so much time in the PHP app and you probably won't be constantly maintaining this app (changing the code), sticking with the PHP code is a great solution.

Railscasts - Free Ruby on Rails Screencasts