Chris
2009-Jun-13 08:36 UTC
What is a simple way to increment the next record (not the "id" column)
Hello Rails community, I have been struggling a bit to try to find a simple solution for this simple requirement and have not found a DRY - simple way for this. I have a table called "Posts" (Articles) with the following attributes: Posts - id - username - post_number How do I make the post_number get incremented for the next record? That is: John creates the first post --- /viewposts/john/1 -- id 1 John creates another post --- /viewposts/john/2 -- id 2 Jane creates the first post -- /viewposts/jane/1 -- id 3 Jane creates the first post -- /viewposts/jane/2 -- id 4 Thank you in advance for your help! Chris
Maurício Linhares
2009-Jun-13 11:39 UTC
Re: What is a simple way to increment the next record (not the "id" column)
First you''ll need an unique index in posts with the user_id and post_number to be sure that they''ll not repeat. then you can just create a before_create callback to define it: class Post < ActiveRecord::Base before_create :set_post_number validates_uniqueness_of :post_number, :scope => :user_id protected def set_post_number write_attribute(:post_number, Post.count( :conditions => { :user_id => self.user_id } ) + 1 ) end end - Maurício Linhares http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr On Sat, Jun 13, 2009 at 5:36 AM, Chris<chrisnow7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hello Rails community, > > I have been struggling a bit to try to find a simple solution for this > simple requirement and have not found a DRY - simple way for this. > > > I have a table called "Posts" (Articles) with the following > attributes: > > Posts > - id > - username > - post_number > > > How do I make the post_number get incremented for the next record? > > That is: > John creates the first post --- /viewposts/john/1 -- id 1 > John creates another post --- /viewposts/john/2 -- id 2 > > Jane creates the first post -- /viewposts/jane/1 -- id 3 > Jane creates the first post -- /viewposts/jane/2 -- id 4 > > > Thank you in advance for your help! > > Chris > > > >
Colin Law
2009-Jun-13 16:25 UTC
Re: What is a simple way to increment the next record (not the "id" column)
2009/6/13 Maurício Linhares <mauricio.linhares-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > First you''ll need an unique index in posts with the user_id and > post_number to be sure that they''ll not repeat. then you can just > create a before_create callback to define it: > > class Post < ActiveRecord::Base > > before_create :set_post_number > > validates_uniqueness_of :post_number, :scope => :user_id > > protected > > def set_post_number > write_attribute(:post_number, Post.count( :conditions => { > :user_id => self.user_id } ) + 1 ) > end > > end >If any existing posts were deleted then would this repeat already used values? Also I think it is generating numbers based on the number of posts for the current user which is not quite what was asked for. Can you not just use an auto-increment column? Colin> - > Maurício Linhares > http://codeshooter.wordpress.com/ | http://twitter.com/mauriciojr > > > > On Sat, Jun 13, 2009 at 5:36 AM, Chris<chrisnow7-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> >> Hello Rails community, >> >> I have been struggling a bit to try to find a simple solution for this >> simple requirement and have not found a DRY - simple way for this. >> >> >> I have a table called "Posts" (Articles) with the following >> attributes: >> >> Posts >> - id >> - username >> - post_number >> >> >> How do I make the post_number get incremented for the next record? >> >> That is: >> John creates the first post --- /viewposts/john/1 -- id 1 >> John creates another post --- /viewposts/john/2 -- id 2 >> >> Jane creates the first post -- /viewposts/jane/1 -- id 3 >> Jane creates the first post -- /viewposts/jane/2 -- id 4 >> >> >> Thank you in advance for your help! >> >> Chris >> >> > >> > > > >
Chris
2009-Jun-13 18:53 UTC
Re: What is a simple way to increment the next record (not the "id" column)
This is a great idea. Thanks a lot. On Jun 13, 4:39 am, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> First you''ll need an unique index in posts with the user_id and > post_number to be sure that they''ll not repeat. then you can just > create a before_create callback to define it: > > class Post < ActiveRecord::Base > > before_create :set_post_number > > validates_uniqueness_of :post_number, :scope => :user_id > > protected > > def set_post_number > write_attribute(:post_number, Post.count( :conditions => { > :user_id => self.user_id } ) + 1 ) > end > > end > > - > Maurício Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > > > On Sat, Jun 13, 2009 at 5:36 AM, Chris<chrisn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello Rails community, > > > I have been struggling a bit to try to find a simple solution for this > > simple requirement and have not found a DRY - simple way for this. > > > I have a table called "Posts" (Articles) with the following > > attributes: > > > Posts > > - id > > - username > > - post_number > > > How do I make the post_number get incremented for the next record? > > > That is: > > John creates the first post --- /viewposts/john/1 -- id 1 > > John creates another post --- /viewposts/john/2 -- id 2 > > > Jane creates the first post -- /viewposts/jane/1 -- id 3 > > Jane creates the first post -- /viewposts/jane/2 -- id 4 > > > Thank you in advance for your help! > > > Chris- Hide quoted text - > > - Show quoted text -
deepak
2009-Jun-15 15:23 UTC
Re: What is a simple way to increment the next record (not the "id" column)
hi, in oracle we can use sequence. in mysql use an autoincrement column, note that you have to save the object so that it hits the database for the column to get updated. Also check out, http://forums.mysql.com/read.php?61,143867,184106#msg-184106 Deepak On Jun 13, 11:53 pm, Chris <chrisn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is a great idea. Thanks a lot. > > On Jun 13, 4:39 am, Maurício Linhares <mauricio.linha...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > wrote: > > > First you''ll need an unique index in posts with the user_id and > > post_number to be sure that they''ll not repeat. then you can just > > create a before_create callback to define it: > > > class Post < ActiveRecord::Base > > > before_create :set_post_number > > > validates_uniqueness_of :post_number, :scope => :user_id > > > protected > > > def set_post_number > > write_attribute(:post_number, Post.count( :conditions => { > > :user_id => self.user_id } ) + 1 ) > > end > > > end > > > - > > Maurício Linhareshttp://codeshooter.wordpress.com/|http://twitter.com/mauriciojr > > > On Sat, Jun 13, 2009 at 5:36 AM, Chris<chrisn...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello Rails community, > > > > I have been struggling a bit to try to find a simple solution for this > > > simple requirement and have not found a DRY - simple way for this. > > > > I have a table called "Posts" (Articles) with the following > > > attributes: > > > > Posts > > > - id > > > - username > > > - post_number > > > > How do I make the post_number get incremented for the next record? > > > > That is: > > > John creates the first post --- /viewposts/john/1 -- id 1 > > > John creates another post --- /viewposts/john/2 -- id 2 > > > > Jane creates the first post -- /viewposts/jane/1 -- id 3 > > > Jane creates the first post -- /viewposts/jane/2 -- id 4 > > > > Thank you in advance for your help! > > > > Chris- Hide quoted text - > > > - Show quoted text -