# Wrong number of arguments (0 for 1) in callback method

**URL:** <https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904>\
**Category:** Rails\
**Created:** [November 13, 2009, 10:00am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904 "2009-11-13T10:00:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 10:00am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/1 "2009-11-13T10:00:25Z")

</div>

## I was creating this simple rail application. my controller method looks like this:

## def create() @simple = Simple.new(params[:simple]) ##params coming from view @simple.save end

And my model class “Simple” as below:

* * *

class Simple \< ActiveRecord::Base  
set\_table\_name “name\_desc\_table”  
set\_primary\_key “name”

before\_create :init\_name

## private def init\_name(cname) self.name = cname end end

When I run this in browser, it throws error “Wrong number of arguments  
(0 for 1)”. Obviously, my method “init\_name” expects one parameter and  
it doesn’t find any while called(by controller??). Basically I want to  
send this ‘cname’ variable from controller to model. Can anybody tell me  
what can I do in controller method so that ‘cname’ variable is passed  
when callback method is called?

Thanks,  
-Kalpesh

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 10:36am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/2 "2009-11-13T10:36:35Z")

</div>

On Nov 13, 9:00 am, Kalpesh1 Patel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> When I run this in browser, it throws error “Wrong number of arguments  
> (0 for 1)”. Obviously, my method “init\_name” expects one parameter and  
> it doesn’t find any while called(by controller??). Basically I want to  
> send this ‘cname’ variable from controller to model. Can anybody tell me  
> what can I do in controller method so that ‘cname’ variable is passed  
> when callback method is called?

Callbacks like before\_create can’t take any arguments

Fred

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 10:39am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/3 "2009-11-13T10:39:06Z")

</div>

Frederick C. wrote:

> On Nov 13, 9:00ï¿½am, Kalpesh1 Patel [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:
> 
> > When I run this in browser, it throws error “Wrong number of arguments  
> > (0 for 1)”. Obviously, my method “init\_name” expects one parameter and  
> > it doesn’t find any while called(by controller??). Basically I want to  
> > send this ‘cname’ variable from controller to model. Can anybody tell me  
> > what can I do in controller method so that ‘cname’ variable is passed  
> > when callback method is called?
> 
> Callbacks like before\_create can’t take any arguments
> 
> Fred

So isn’t there any way I can pass some value from controller to model?

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 11:09am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/4 "2009-11-13T11:09:07Z")

</div>

Hi Kalpesh1 Patel

> So isn’t there any way I can pass some value from controller to model?

```
You can define cname as n attr_writer in model and can set its value 

```

from controller like @simple.cname = ‘namehere’

Sijo

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 12:00pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/5 "2009-11-13T12:00:30Z")

</div>

Sijo k g wrote:

> Hi Kalpesh1 Patel
> 
> > So isn’t there any way I can pass some value from controller to model?
> 
> ```
> You can define cname as n attr_writer in model and can set its value 
> 
> ```
> 
> from controller like @simple.cname = ‘namehere’
> 
> Sijo

Thanks sijo! I am able to get/set it using attr\_accessor now 🙂

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 11:15am UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/6 "2009-11-13T11:15:48Z")

</div>

On Fri, Nov 13, 2009 at 10:39 AM, Kalpesh1 Patel  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > when callback method is called?
> > 
> > Callbacks like before\_create can’t take any arguments
> > 
> > Fred
> 
> So isn’t there any way I can pass some value from controller to model?  
> What do you mean? Of course you can pass values, in fact, you’re  
> passing a value there.  
> I don’t see the need of doing something like the before\_create hook.  
> Why don’t you just pass the value to the constructor? Simple.new(:name  
> =\> params[:simple])

–  
Leonardo M…  
There’s no place like ~

---

<div class="post-metadata">

**Author:** ![kalpesh13131](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c57346/32.png) [@kalpesh13131](https://www.ruby-forum.com/u/kalpesh13131)\
**Post date:** [November 13, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/wrong-number-of-arguments-0-for-1-in-callback-method/178904/7 "2009-11-13T12:10:45Z")

</div>

Leonardo M. wrote:

> On Fri, Nov 13, 2009 at 10:39 AM, Kalpesh1 Patel  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > > when callback method is called?
> > > 
> > > Callbacks like before\_create can’t take any arguments
> > > 
> > > Fred
> > 
> > So isn’t there any way I can pass some value from controller to model?  
> > What do you mean? Of course you can pass values, in fact, you’re  
> > passing a value there.  
> > I don’t see the need of doing something like the before\_create hook.  
> > Why don’t you just pass the value to the constructor? Simple.new(:name  
> > =\> params[:simple])
> 
> –  
> Leonardo M…  
> There’s no place like ~

I need before\_create because I want to do some preprocessing everytime i  
save my ‘Simple’ state in the DB. For example, authentication, logging  
or similar stuff I will perform just before ‘save’(this might raise  
another question, is this right approach? but this can be a separate  
discussion). I thought of passing the value in the callback method  
itself but now I am able to do it through attr\_accessor. Thanks Leo for  
the suggestion 🙂

-Kalpesh
