views:

85

answers:

1

Hey all, I get a sitenum undefined method error. Problem is I am trying to target site_id and student_number and then increment value of student_number by 1 based on site_id. So there will be at least two records with a 2001 value for student_number if there are two site_ids (e.g. site_id 1, site_id 2). And if a value of 2001 exists for that site, then I would like to increment by 1 so next student_number for that site will be 2002, 2003, etc.

Student Model:

:student_number =>
 sitenum = self.site_id
 count = Student.count_by_sql("SELECT MAX(student_number) FROM students WHERE site_id = #{sitenum}")
 if count >= 2001
     Student.sitenum(:order => "student_number DESC").student_number + 1
  else
    2001
  end

Any response would be greatly appreciated.

A: 

I understood nothing from a description, but suppose, you want this that way:

:student_number =>
  sitenum = self.site_id
  count = Student.count_by_sql("SELECT MAX(student_number) FROM students WHERE site_id = #{sitenum}")
  if count >= 2001
    Student(:condition => { :site_id => sitenum }, 
      :order => "student_number DESC").student_number + 1
  else
    2001
  end
Vestel
Actually, this returns a value of 1 every time, if 2001 already exists for the site. My intention is if 2001 already exists, then increments every time a new record is created for that site (e.g. 2002, 2003, 2004, etc). Right now if 2001 exists, it just returns 1 very time. Note I had to use Student.first because Student(:condition will return an error that Student is not a method.
JohnMerlino