Is there a way in which I can create a serial number (SNO) column through scaffolding in Rails which increases on adding a record, decreases on deleting a record and cannot be modified manually?
Asked
Active
Viewed 514 times
0
2 Answers
1
It's not clear whether there is any relationship involved, but it sounds like counter_cache may be a good fit.
A Railscast episode provides the code examples and a video tutorial.

Douglas F Shearer
- 25,952
- 2
- 48
- 48
0
If you want to create a summary column on a model you will need to put this logic into your models. There is not a built in method for this (like a standard autoincrement field), but it can be added easily:
class Parent << ActiveRecord::Base
# Contains a field: summary_field
end
class Child << ActiveRecord::Base
after_save => :increment_summary
before_destroy => :decrement_summary
def increment_summary
Parent.find(self.id).summary_field.increment
end
def decrement_summary
Parent.find(self.id).summary_field.decrement
end
end

Mike Buckbee
- 6,793
- 2
- 33
- 36