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.
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.
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=’
Can you show the full error message?
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