I have an application that sends messages and each message that gets sent needs to have a unique sequence number, this sequence number is incremented with each message. As I understand it, I should be storing this sequence number in the DB rather than in a global variable. My question is, how do I use rails and active record to define this table? The table doesn''t need to have any of the active record things like an auto incrementing integer ID, it justs needs to be a single column, single row table storing an integer value that I can easily increment. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 Feb 2008, at 12:45, Tim Conner wrote:> I have an application that sends messages and each message that gets > sent needs to have a unique sequence number, this sequence number is > incremented with each message. As I understand it, I should be > storing > this sequence number in the DB rather than in a global variable. > > My question is, how do I use rails and active record to define this > table? The table doesn''t need to have any of the active record things > like an auto incrementing integer ID, it justs needs to be a single > column, single row table storing an integer value that I can easily > increment.Why not just do the following in your Message model: before_save :assign_sequence_number protected def assign_sequence_number last_number = self.class.maximum(''sequence_number'', :conditions => [<add conditions if you want>]) if last_number.nil? self.sequence_number = 1 else self.sequence_number = last_number+1 end end Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I did think of finding the maximum of all the sequence numbers in the table and then incrementing it, I just thought that it''d be quicker (as the message table grows large) to grab the number from a separate table which only stores the last number used. I''ll implement it using your method for now, I can always revisit it if there is a performance concern. thanks for the help -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 25 Feb 2008, at 13:01, Tim Conner wrote:> I did think of finding the maximum of all the sequence numbers in the > table and then incrementing it, I just thought that it''d be quicker > (as > the message table grows large) to grab the number from a separate > table > which only stores the last number used. > I''ll implement it using your method for now, I can always revisit > it if > there is a performance concern.You can also implement a Preference model. I tend to favour a multirecord preference table, where each record signifies one particular preference, in your case this preference will be current_counter_value which is an integer. I tend to mask this Preference model behind a few convenience methods, similar as to what the restful_authentication plugin does with the current_user method, i.e. providing you with a method like "counter_number", which automatically returns the current counter and increases the preference counter by one. You could check out the settings plugin at http://beautifulpixel.com/ 2006/02/25/settings-plugin, although i haven''t used it myself. Best regards Peter De Berdt --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
single row table makes no sense why not create Message model, backed with messages table, having all those activerecord things, like id (which is in fact unique sequence number), and other message attributes? On 25 фев, 13:45, Tim Conner <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I have an application that sends messages and each message that gets > sent needs to have a unique sequence number, this sequence number is > incremented with each message. As I understand it, I should be storing > this sequence number in the DB rather than in a global variable. > > My question is, how do I use rails and active record to define this > table? The table doesn''t need to have any of the active record things > like an auto incrementing integer ID, it justs needs to be a single > column, single row table storing an integer value that I can easily > increment. > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---