This is what I did to determine how many users and which user is online and active in the last 30 minutes from Rails. I went through http://groups.google.com/group/rubyonrails-talk to find out how other people dealt with this issue. Most people deal with this through session with active_record_store. I think this way is quite reasonable because each time the user request to our pages, it will create a new record or update the existing record. This would allow me and other people interferes to determine which user is online.
To configure to use active_record_store with session, go to see Agile Web Development with Rails 2nd edition on page 99.
Some people are likely to interfere by adding the user_id and host column in the table sessions. They use before_filter on the application controller. If they found the session_id in the table is the same as session.session_id, they update the column user_id with session[:user_id]. To access to the sessions table, it must use this syntax CGI::Session::ActiveRecordStore::Session. I try with this syntax but could not update my fields anyway. It is difficult to interfere with code the rails creator use to write a record to the sessions table. It is not the normal active record model that I expected. So, I come up with another solution by creating a new model called Session that maps it to the sessions table. This way I could update the record in the table. But I still face with another problem. I need to write to the table whenever the user first request to my page with the new column "host" that record ip address. I don't know how to interfere with this.
Therefore, I find another solution that seems to be not so good, but reasonable. I create a new table that map to the sessions table called active_users that has the following fields: id, session_id, user_id, host, url, created_at, updated_at. I use after_filter on the application controller to update or create a new record in this table by looking into the session_id in the table first. If it is already exists, I just update the host, user_id, and url. Otherwise, I create a new row in this table. I also create a trigger on the sessions table. When the user deletes the records, it also deletes record in active_users also. I think I should need a cron job that removes all unnecessary session records every 1 hour. Oh, there is still another problem, when the user log out, don't forget to delete records in active_users table. There is no need for the sessions table because it will be done automatically. And, that's it. I create two methods in the User model.
def self.total_online
sql = "Select Count(Distinct user_id) as total " +
"From active_users " +
"Where updated_at > ?"
ActiveUser.find_by_sql([sql, 30.minutes.ago])[0].total.to_i
end
def self.online?(user_id)
sql = "Select * from active_users " +
"Where user_id = ? " +
"And updated_at > ?"
ActiveUser.find_by_sql([sql, user_id, 30.minutes.ago]).length >= 1
end
Web 2.0, Ruby, Rails, TDD, BDD,
rSpec, XMPP, BOSH, REST,
Javascript, Ajax, Google Maps API,
Google Gears API, CSS,
Java, C#, Social Network, RMagick, Nginx
Tuesday, October 23, 2007
Friday, October 19, 2007
Validate field that doesn't exist in the table
In order to validate such a field like this in Rails, you must create a virtual attribute in the model and then use a normal validation routine in the model.
validates_presence_of : house_number
def house_number
@house
end
def house_number=(house_number)
@house = house_number
end
Otherwise, you have to manually validate by yourself.
validates_presence_of : house_number
def house_number
@house
end
def house_number=(house_number)
@house = house_number
end
Otherwise, you have to manually validate by yourself.
Labels:
ruby on rails
Tuesday, October 16, 2007
Sending SMS messages from your Rails application
This is the title of one post I found on http://www.lukeredpath.co.uk/2007/8/29/sending-sms-messages-from-your-rails-application that allows the user to send sms messages from computer to any mobile phones on the earth. You can try, it allows 10 credit for testing, but you must sign up for their API key.
One such provider is Clickatell who are one of the bigger providers out there with a range of services whose customers include Barclays Bank, the BBC and CNN.
One such provider is Clickatell who are one of the bigger providers out there with a range of services whose customers include Barclays Bank, the BBC and CNN.
Labels:
ruby on rails
Thursday, October 4, 2007
Viewport properties
Here is the link that talk about the viewport of the browser such as inner dimension of the window, scrolling offset, and page height. This link talks more details than my previous post. Check it out: http://www.quirksmode.org/viewport/compatibility.html, by Quirksmode.org.
Labels:
javascript
Wednesday, October 3, 2007
9 tricks on Web Development
1. If you upload files via iframe, and you want to cancel the uploading process, set src attributes of the iframe to 'blank.html' or 'about:blank'.
2. To begin uploading, use $('form').submit();
3. Use min-height for firefox and height for IE for setting the minimum height of the container
4. When finished adding record to the database, redirect_to the page itself or other pages otherwise the browser still contain the post data and if the user press F5, it will post the previous data to the server again. For rails don't use render :action, instead use redirect_to :action
5. For Update, you need to check the errors collection to make sure your data is fully validated because it doesn't raise any exceptions when the data failed the validation.
6. Rails doesn't support render more than one action.
7. Don't suppose redirect_to will end the action. If you want to this way, add "and return" after redirect_to.
8. GROUP_CONCAT function cannot used with other datatypes except String. To convert to string, use CAST(field AS CHAR).
9. The onchange event of select html tag doesn't fire when the user scroll the list using keyboard on Firefox, to solve add keyup event.
2. To begin uploading, use $('form').submit();
3. Use min-height for firefox and height for IE for setting the minimum height of the container
4. When finished adding record to the database, redirect_to the page itself or other pages otherwise the browser still contain the post data and if the user press F5, it will post the previous data to the server again. For rails don't use render :action, instead use redirect_to :action
5. For Update, you need to check the errors collection to make sure your data is fully validated because it doesn't raise any exceptions when the data failed the validation.
6. Rails doesn't support render more than one action.
7. Don't suppose redirect_to will end the action. If you want to this way, add "and return" after redirect_to.
8. GROUP_CONCAT function cannot used with other datatypes except String. To convert to string, use CAST(field AS CHAR).
9. The onchange event of select html tag doesn't fire when the user scroll the list using keyboard on Firefox, to solve add keyup event.
Labels:
ruby on rails
Subscribe to:
Posts (Atom)