Wrong number of arguments (2 for 1) error message help


#1

All,
This is really starting to get me as i can’t figure out why I am
getting this error. Here is what is going on. I have a table called
items and a item controller. When I add a new item to the items table
I am also creating several associated tables. the code for this so
that you can follow along is:

def create

 @item = Item.new(params[:item])
 # The item belongs to the user
@user.reload
@item.user = @user
 if @item.save

     itemtablename = "Item_#{@item.id}"

   ActiveRecord::Schema.define do
       create_table(itemtablename, :options => 'ENGINE=InnoDB

DEFAULT CHARSET=utf8’) do |t|
t.column :field_type, :string, :null => false
t.column :body, :text, :null => true
t.column :required, :tinyint, :null => false, :default => ‘0’
t.column :demographic, :tinyint, :null => false, :default
=> ‘0’
t.column :image, :text, :null => true
end
end
# right now we are displaying this flash for debugging purposes
will probably remove it in the near future
flash[:notice] = ‘Item was successfully created.’
$itemnew = @item
redirect_to :action => ‘page2’
else
render :action => ‘new’
end
end

I am creating $itemnew as i could not get the item to pass on to the
page2 action any other way. that code is here (this is where I have
the problem)

called when the user clicks on next from the first page

def page2
@generic_elements = GenericElement.find(:all)
@newitem = Item.find(:first,
:conditions => “id = ‘#{$itemnew.id}’ and user_id =
‘#{@user.id}’”)
itemtablename = “Item_#{@newitem.id}”
#ItemDesign.new(“Item_#{@newitem.id}”)
@itemdesigns = ItemDesign.find_all_elements(itemtablename)
render :action => ‘page2’
end

Now the ItemDesign is a model class that i created myself right now
it looks like this in its entirety:

class ItemDesign < ActiveRecord::Base
attr_accessor :table_name

def self.find_all_elements(table_name)
@table_name = table_name
find_by_sql(“select * from ?”, @table_name)
end
end

The problem is that I am getting the wrong number of arguments (2 for

  1. error that is in the subject. I have tried an initialize
    definition in the model class as well as just about everything else
    that I can think of, which is not much since this is my first time on
    rails.

Any help is greatly appreciated I really love the language and am
hopefully going to be able to contribute with answers to the list soon.

Andrew


#2

Hi Andrew,

You’re holding on to an ActiveRecord object (item) between requests
(sticking it into $itemnew), right?

And you get this problem in development mode?

Assuming you answer ‘yes’ then I’d say “don’t do that”. ActiveRecord
objects do not survive between requests in development mode.

Also, in production mode you’ll likely have more than one rails process
servicing requests - you can’t trust that the right process will get
the user’s next request to pick up $itemnew when the next page action
is called.

With that in mind I’d advise you to put the @item into the users’
session (which will require you to have a “model ‘item’” line at the
top of your controller - in ApplicationController is the safest, imho).

Regards,
Trevor

On 17-Nov-05, at 4:53 PM, Andrew F. wrote:

# The item belongs to the user
  			t.column :body, :text, :null => true
  redirect_to :action => 'page2'

def page2
Now the ItemDesign is a model class that i created myself right now it
end

Andrew


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


Trevor S.
http://somethinglearned.com


#3

Trevor,
thanks for that tip. I knew that adding the $itemnew in there was
going to end up causing me a problem but like I said I am new to this
language and am just trying to get things to work as opposed to
working the right way, figured once I had it working I could than go
back and figure out what dumb mistakes I made.

Based on that tip would adding the name of the table that I created
into the session also solve the issue that I have with the wrong
number of arguments? or is that something completely different.

Guess it is time to go and figure out what you actually mean by
adding model item into my app controller. My copy of Agile Programers
is getting well dog eared.

Andrew


#4

Oh yeah and the answer to your question is yes I am only working in
dev mode right now. Wanted to get the stub of the whole project done
before I started looking at testing and production. One thing at a
time is all I can handle right now picking up a new language.

Andrew


#5

Thanks for the help however now I am getting a new error and not sure
how to fix it at all. It seems that the quotes are being passed to
the SQL statement as well. This is causing MySQL to throw a fit. Is
this an issue with MySQL or with Rails? why would the quotes be
getting passed as part of the string. This is he code that I have to
make the call:

in my model:

def self.find_all_elements(tablename)
tablename2 = tablename
find_by_sql [‘SELECT * FROM ?’,tablename2]
end

In my Controller:
itemtablename = “Item_#{@newitem.id}”
@itemdesigns = ItemDesign.find_all_elements(itemtablename)

I am running on the most recent stable build of rails and ruby (not
the release candidate). Also running the most recent MySql v 5

Thanks again for all the help. Off to google to try and figure this
out as well.

Andrew


#6

Andrew,

WRT your error, you should make the argument into square brackets
eg
find_by_sql( [“select * from ?”, @table_name] )

The method is expecting only one argument. Hence the 2 for 1 error.

Cheers


#7

Never mind figured this one out. had to create a new string var that
had the sql string in it before using the find_by_sql as rails is
trying to protect me from sql injection attacks.

Andrew