Undefined method error

rid.database_columns[session_db_array[0]]

When i execute this line, I am getting an error

undefined method `database_columns' for #<Rid:0x14e691e>

puts(database_columns[session_db_array[0]]) gives me the correct value .
The value is Title

I need to assign rid.Title = ...something

What do I do to get rid of the undefined method error

···

--
Posted via http://www.ruby-forum.com/.

rid.database_columns[session_db_array[0]]

You're calling database_columns on the rid object here.

When i execute this line, I am getting an error

undefined method `database_columns' for #<Rid:0x14e691e>

puts(database_columns[session_db_array[0]]) gives me the correct value .

You're calling database_columns without an explicit receiver here. So the method is being discovered according to the normal "up and to the right" lookup rules.

···

On Jul 17, 2012, at 5:13 AM, deal bitte <lists@ruby-forum.com> wrote:

The value is Title

I need to assign rid.Title = ...something

What do I do to get rid of the undefined method error

--
Posted via http://www.ruby-forum.com/\.

Hi,

Well, obviously the "Rid" object (whatever that is) doesn't have a
"database_column" method, while "self" does. So the solution is to
simply call the method on the correct object.

What is self.class?

···

--
Posted via http://www.ruby-forum.com/.

rid = Rid.new
database_columns=['ExternalRef', 'Originator', 'Responder', 'Title',
'Category', 'Location', 'Observation',
'Recommendation','Concern','Document_id', 'OriginatorRef']
rid.database_columns[0]

Well this is my code. I am new to ruby.

···

--
Posted via http://www.ruby-forum.com/.

Like I said, you have to make sure you're calling the method on the
right object.

It seems you want to call the method on "self", so instead of
"rid.database_columns" you have to write "database_columns" without a
receiver:

rid = Rid.new
database_columns = [
  'ExternalRef',
  'Originator',
  'Responder',
  'Title',
  'Category',
  'Location',
  'Observation',
  'Recommendation',
  'Concern','Document_id',
  'OriginatorRef'
]
database_columns[0]

···

--
Posted via http://www.ruby-forum.com/.

OK, I see what you mean. You want to use the values of
"database_columns" as method names.

However, you cannot simply write ".database_columns[0]". This will be
interpreted as calling the method "database_columns". You have to use
"send" or "public_send", which allows you to pass the method name as a
string:

rid.send(database_columns[0], '(value for title)')

···

--
Posted via http://www.ruby-forum.com/.

I cannot create a hash as I do not add all columns in database. It
varies based on my values

rid = Rid.new
database_columns=['ExternalRef', 'Originator', 'Responder', 'Title',
'Category', 'Location', 'Observation',
'Recommendation','Concern','Document_id', 'OriginatorRef']
session_db_array=[1,3,4,5,6,7,9] #this value varies and is not fixed
data_save_array = params[:c].collect{|value| value} #this value varies
session_db_array.length.times do |i|
  rid.send(database_columns[session_db_array[i]],data_save_array[i])
  rid.date = Time.new
  rid.workflow = 0
  rid.project_id = session[:projectid]
end
rid.save

···

--
Posted via http://www.ruby-forum.com/.

This makes no sense. If anything Ruby would complain about *2* arguments
for 0. Is this error really caused by "send"?

Please post the whole error message and your actual code.

···

--
Posted via http://www.ruby-forum.com/.

Trace:

C:/daten/projects/pmdb/app/controllers/rids_controller.rb:157:in
`Originator'

as Originator is the first mapped value.

···

--
Posted via http://www.ruby-forum.com/.

Well, if you don't want to post the whole error message and the code,
you'll have to do the debugging yourself. A random entry from the stack
trace doesn't help.

Try "__send__" in case the "send" method has been overwritten (which is
highly unlikely). Also try fixed values for testing like
"rid.send(:Title, 'xyz')".

···

--
Posted via http://www.ruby-forum.com/.

Well I think i will just go with your hash idea
Atleast i go further with this

···

------------------------------------------------------------
database_columns=['ExternalRef', 'Originator', 'Responder', 'Title',
'Category', 'Location', 'Observation',
'Recommendation','Concern','Document_id', 'OriginatorRef']
session_db_array=[1,3,4,5,6,7,9]
myhash = {}
db_columns.each_with_index{|k,i|myhash[k] = data_save_array[i] }
# This is the hash of selected db columns and their corresponding values
rid = Rid.new myhash
rid.date = Time.new
rid.workflow = 0
rid.project_id = session[:projectid]
rid.save
------------------------------------------------------------------------
But the problem is that
My hash values do not correspond to my sql query
eg: In my hash
"Originator"=>" J FABRE 2 originator"
But in my insert query
Originator = 0
------------------------------------------------------------------------
puts(myhash.inspect) value
{"Originator"=>" J FABRE 2 originator", "Title"=>"Element development
plan", "Category"=>"major category", "Location"=>"prob loca08.01.2012",
"Observation"=>"2.0", "Recommendation"=>"add GACF milestone in schedule
section 6.3", "Document_id"=>"Element development plan"}
------------------------------------------------------------------------

INSERT INTO `rids` (`ExternalRef`, `Originator`, `Responder`, `Title`,
`Category`,
`Location`, `Observation`, `Recommendation`, `Conclusion`, `Response`,
`InternalState`, `InternalDate`, `ExternalState`, `MeetingReference`,
`Disposition`,
`DispositionState`, `Concern`, `Requirement_id`, `Ridresponse_id`,
`Document_id`,
`project_id`, `date`, `closuredate`, `workflow`, `OriginatorRef`,
`Implementation`, `implemented_on`, `implemented_by`, `closed_comment`)
VALUES(NULL, 0, 0, 'Element development plan', 0,
'prob loca08.01.2012', '2.0', 'add GACF milestone in schedule section
6.3', NULL, NULL,
0, NULL, 0, NULL, NULL,
NULL, NULL, NULL, NULL, 0,
20, '2012-07-17 15:51:11', NULL, 0, NULL,
NULL, NULL, NULL, NULL)

--
Posted via http://www.ruby-forum.com/.

Problem solved. Thank you all :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

Ya.Thats there. But i want to do rid.save at the end and save the values
to the database
rid = Rid.new ( Mytable name is rids )
rid.Originator = bla bla
rid. Title = bla bla
rid.save ( to save all of them in the database )

Jan E. wrote in post #1069039:

···

Like I said, you have to make sure you're calling the method on the
right object.

It seems you want to call the method on "self", so instead of
"rid.database_columns" you have to write "database_columns" without a
receiver:

rid = Rid.new
database_columns = [
  'ExternalRef',
  'Originator',
  'Responder',
  'Title',
  'Category',
  'Location',
  'Observation',
  'Recommendation',
  'Concern',
  'Document_id',
  'OriginatorRef'
]
puts database_columns[0]

--
Posted via http://www.ruby-forum.com/\.

Exactly. Sorry that i had not posted my question properly.
So I shall try putting the code below
The column names and the value to insert in the columns are both in
arrays :):slight_smile:
rid.send(database_columns[0],data_save_array[0])

Thank you so much for the help

Jan E. wrote in post #1069041:

···

OK, I see what you mean. You want to use the values of
"database_columns" as method names.

However, you cannot simply write ".database_columns[0]". This will be
interpreted as calling the method "database_columns". You have to use
"send" or "public_send", which allows you to pass the method name as a
string:

rid.send(database_columns[0], '(value to insert for this column)')

By the way, forget what I said about calling "database_columns". This is
of course a variable and not a method (which we couldn't tell from your
first post)

--
Posted via http://www.ruby-forum.com/\.

I am getting an error on using .send
wrong # of arguments(1 for 0)

deal bitte wrote in post #1069050:

···

I cannot create a hash as I do not add all columns in database. It
varies based on my values

rid = Rid.new
database_columns=['ExternalRef', 'Originator', 'Responder', 'Title',
'Category', 'Location', 'Observation',
'Recommendation','Concern','Document_id', 'OriginatorRef']
session_db_array=[1,3,4,5,6,7,9] #this value varies and is not fixed
data_save_array = params[:c].collect{|value| value} #this value varies
session_db_array.length.times do |i|
  rid.send(database_columns[session_db_array[i]],data_save_array[i])
  rid.date = Time.new
  rid.workflow = 0
  rid.project_id = session[:projectid]
end
rid.save

--
Posted via http://www.ruby-forum.com/\.

The error is in .send only

If i give
rid.send(database_columns[session_db_array[i]]) instead of
rid.send(database_columns[session_db_array[i]],data_save_array[i])
the error vanished. But I have to give the second parameter .

Jan E. wrote in post #1069055:

···

This makes no sense. If anything Ruby would complain about *2* arguments
for 0. Is this error really caused by "send"?

Please post the whole error message and your actual code.

--
Posted via http://www.ruby-forum.com/\.

deal bitte wrote in post #1069042:

The column names and the value to insert in the columns are both in
arrays :):slight_smile:
rid.send(database_columns[0],data_save_array[0])

It's better to use a hash instead of two arrays. You can even pass this
hash directly when creating the object:

values = {
  'ExternalRef' => 'some value',
  'Originator' => 'some value',
  'Responder' => 'some value',
  'Title' => 'some value',
  'Category' => 'some value',
  'Location' => 'some value',
  'Observation' => 'some value',
  'Recommendation' => 'some value',
  'Concern' => 'some value',
  'Document_id' => 'some value',
  'OriginatorRef => 'some value''
}
rid = Rid.new values
rid.save

(I'm assuming this is ActiveRecord in Rails)

···

--
Posted via http://www.ruby-forum.com/\.