views:

71

answers:

3

Hey all,

I'm just trying to increment a record by 1 starting at 2000, when a new record is created upon clicking on the create action to create a record:

if resource_model == Student then @resource.testing_id = id + 2000 end

So if the record has an id of 1, I assume that the testing_id will be 2001. But instead it returns: 2147483647 (maximum mysql limit?)

Any suggestions on how to address this? Thanks.

A: 

2147483647 = 2 ^(32-1) Could you show some of your code here?

From what i'm guessing here is that you are comparing apples with strawberries :P. I think you are adding a "1" on a string so that means for example:

2000 + 1 = 20001 20001 + 1 = 200001

So if you do that a couple of times this means you will get to the maximum size of an int (21475483647). I don't know this for a 100% sure, but this has to do with the way you ask your question and the way you don't post code etc... I hope this edit was helpfull tho.

Younes
def create_common @resource = yield params[resource_params_key] #This above just captures the user input. think of it like params[:student], and then assigns it to an instance variable @resource. if resource_model == Student then @resource.testing_id = Resource.first(:order => "testing_id DESC").testing_id + 1 end #This code above is where the problem is. If I replace Resource with resource_model (Student), then it still returns: 2147483647. I try using this: 2 ^(32-1) and it still returns 2147483647. respond_to do |format| if save_resource(@resource)
JohnMerlino
Your comment is totaly useless. It is unreadable. If you want to add some code, please edit your question, put there your code and format it correctly (add four spaces before every code line, or use 101 button)
klew
def create_common @resource = yield params[resource_params_key]#This above just captures the user input. think of it like params[:student], and then assigns it to an instance variable @resource.if resource_model == Student then @resource.testing_id = Student.first(:order => "testing_id DESC").testing_id + 1 end respond_to do |format|if save_resource(@resource) Hope this is more readable.
JohnMerlino
Eh... :) Please edit your question, don't add it as a comment
klew
A: 

Object.idf is a (deprecated) method that returns the Ruby object ID, which uniquely identifies the object. Rails has overridden id so that in models it refers to the database primary key. I'm going to take a guess that your code snippet is from a controller, and that's why id is returning a strange and large number: It's the Ruby object id of the controller, not the primary key of the object. Give id a receiver, e.g. @resource.id, and see how that works.

Wayne Conrad
This:if resource_model == Student then @resource.testing_id = @resource.id + 3000 endreturns this:You have a nil object when you didn't expect it!You might have expected an instance of Array.The error occurred while evaluating nil.+
JohnMerlino
Ah, you're creating @resource. As klew said, it won't have an ID until you save it. Go with klew's answer. It's good.
Wayne Conrad
+1  A: 
  1. You can't know record ID during create. ID is known after saving record do database.

  2. You can't relay on ID to give you values like 1, 2, 3 ... and so on.

  3. Don't store value like ID+2000, becouse you can get it at any time by calculating id+2000.

You can get next testing_id by something like this:

if resource_model == Student then
  @resource.testing_id = Student.first(:order => "testing_id DESC").testing_id + 1
end

But if two processes at the same time will fetch the same value then you will have duplicate testing_id.

klew
This:if resource_model == Student then @resource.testing_id = Resource.first(:order => "testing_id DESC").testing_id + 1endreturns this:uninitialized constant RestfulControllerActions::InstanceMethods::ResourceWhat exactly did you mean by Resource? Note that this is centralized code, not in a specific controller but in a controller inherited by all controllers. So resource_model refers to the modelof the current controller. In this case, it's student.
JohnMerlino
Ok, changed Resource to Student. In Rails you usualy use the same name for variable as for model from which you get records, so I automaticaly thought that you have somewhere `@resource = Resource.create` or something like this :)
klew
Yeah, I changed it to student: if resource_model == Student then @resource.testing_id = Student.first(:order => "testing_id DESC").testing_id + 1endbut this also returns: 2147483647
JohnMerlino
Go to your console and type: `Student.first(:order => 'testing_id DESC').testing_id` and see what it gives you. Probably you already saved some records with testing_id like `2147483647` and that's why it gives you that value again
klew
Thanks, you were right. But what if I'm creating the very first record in the Student table: if resource_model == Student then @resource.testing_id = if Student.id == 1 2000 else Student.first(:order => "testing_id DESC").testing_id + 1 end endThis returns:You have a nil object when you didn't expect it!The error occurred while evaluating nil.testing_id
JohnMerlino
Is there a way to check if a database has a record in the if statement?
JohnMerlino
Alright I figured it out. I just reversed the if and else. Thanks a lot klew for helpful advice.
JohnMerlino