Forum: Ruby on Rails wrong number of arguments (2 for 1) error message help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 a.filipowski (Guest)
on 2005-11-18 01:55
(Received via mailing list)
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
27c170f482104299af279902be0a9c26?d=identicon&s=25 trevor (Guest)
on 2005-11-18 02:34
(Received via mailing list)
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 Filipowski 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Trevor Squires
http://somethinglearned.com
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 a.filipowski (Guest)
on 2005-11-18 02:49
(Received via mailing list)
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
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 a.filipowski (Guest)
on 2005-11-18 02:52
(Received via mailing list)
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
9d1f5d2d9de70bd9a934f557dc95a406?d=identicon&s=25 Daniel ----- (liquid)
on 2005-11-18 03:10
(Received via mailing list)
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
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 a.filipowski (Guest)
on 2005-11-18 15:39
(Received via mailing list)
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
11bd67aedc131020a51440fad59116cc?d=identicon&s=25 a.filipowski (Guest)
on 2005-11-18 16:15
(Received via mailing list)
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
This topic is locked and can not be replied to.