# Undefined method â€˜câ€™ error

**URL:** <https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977>\
**Category:** Rails\
**Created:** [February 5, 2010, 2:14pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977 "2010-02-05T14:14:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 5, 2010, 2:14pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977/1 "2010-02-05T14:14:45Z")

</div>

Hey all,

I’m getting an undefined method ‘c’ error. I’m in the students  
controller, and I’m trying to find the book associated with the student,  
and then take that book’s id\_number and assign the room’s id to it and  
then save the book:

def student\_test  
@student = (params[:id] and Student.find(params[:id])) || Student.new  
run\_sequence :testize  
end

def test\_finalize  
Student.transaction do  
@student.update\_attributes(params[:student]) and @student.test! if  
(params[:student])

```
room = Room.new

room.room_num = 5
room.save

c = Student.for_test && @student #@student.for_test doesn't work,

```

but I’m trying to apply named\_scope to @student to get book associated  
with the selected student.  
Book.c.id\_num = room.id  
Book.save

end  
end

Student model:

named\_scope :for\_test, :include =\> :book, :conditions =\> [:first,  
“book.location\_id == site\_id”]

The idea is to find the book associated with the student via the  
location and then pass the room id into that book’s id\_num.

Thanks for any response.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 5, 2010, 3:54pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977/2 "2010-02-05T15:54:16Z")

</div>

John M. wrote:

Given I see lots of issues with your code, I’m going to answer your  
question directly.

> ```
> Book.c.id_num = room.id
> 
> ```

This line references a class method on the Book class named c. Something  
like this:

class Book  
def self.c  
…  
end  
end

This could also be defined using a named\_scope, which essentially  
becomes a class method:

class Book  
named\_scope :c, …  
end

So if Book has no c method defined, and nothing creates one at runtime,  
then Ruby gives you the error you’re seeing.

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 5, 2010, 4:57pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977/3 "2010-02-05T16:57:06Z")

</div>

I changed it around:

def student\_test  
@student = Student.for\_test.find(params[:id]) if params[:id]  
@student ||= Student.new  
run\_sequence :testize  
end

def test\_finalize  
Student.transaction do  
if (params[:student]) and @student.update\_attributes(params[:student])  
@student.test!  
end

room = Room.new(:room\_num =\> 5)  
room.save

book = @student.book  
book.id\_num = room.id  
book.save  
end  
end

But still get this error message:  
undefined method `id\_num=’

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 5, 2010, 5:11pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977/4 "2010-02-05T17:11:17Z")

</div>

Can you show the full error message?

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [February 5, 2010, 5:28pm UTC](https://www.ruby-forum.com/t/undefined-method-a-ca-error/182977/5 "2010-02-05T17:28:40Z")

</div>

Sharagoz – wrote:

> Can you show the full error message?

In the code, I actually have:  
book = @student.books  
not  
book = @student.book

This is because in Student model, I have has\_many :books

And named\_scope:  
named\_scope :for\_test,  
:include =\> :books, :conditions =\> [“books.location\_id = site\_id  
&& books.book\_state\_id = 5”]

And this is the full error:  
NoMethodError in StudentsController#student\_test  
undefined method `id\_num=’ for #Class:0x106036ea0
