2013-03-28 3 views
0

제 모델 중 NoMethodError에 문제가 있습니다. 로그 파일에서 Mongo + RoR 모델. NoMethodError가 붙어 있습니다.

, 우리는이 :

NoMethodError (undefined method `length=' for #<Book:0x000000083866b8>): 
2013-03-28T10:25:19+00:00 app[web.1]: app/models/engine/book.rb:13:in `block in find_or_create_by_guide' 
2013-03-28T10:25:19+00:00 app[web.1]: app/models/engine/book.rb:9:in `find_or_create_by_guide' 

이 나에게 중요한 모든 파일을 통해 가자.

class Guide::Document 
    include MongoMapper::Document 
    key :city, Integer 
    key :trips, Array 
    key :themes, Array 
    key :places, Array 
    key :pace, String 
    key :date_from, Time 
    key :date_to, Time 
    key :host, String 
    key :length, Integer 
    timestamps! 
end 

그런 책 모델이 가이드 문서에 호출됩니다 :

나중에 book.rb에서
module ClassMethods 
    def find_or_create_by_guide(guide) 
    book = ::Book.find_or_create_by_document(guide.id.to_s) do |b| 
     b.city_id = guide.city 
     b.host = guide.host 
     b.pace = guide.pace || :normal 
     b.length = guide.length 
    end 

, 내가 가진 시작에 대한

, 우리는 몽고의 document.rb이 다음 줄 :

groups = sorted_points.in_groups_of(self.length.count, false) 

Length.rb :

,691
class Length < ActiveRecord::Base 
    belongs_to :book 

    attr_accessible :book_id 
end 

Book.rb 363,210 :

attr_accessible :user_id, :country_id, :city_id, :hotel_id, :type, :price, :host, :pace, :created_at, :updated_at, :length 

마지막 길이 마이그레이션 :

class AddLengthColumnToBooks < ActiveRecord::Migration 
    def change 
    add_column :books, :length, :integer 
    end 
end 

어떤 힌트 또는 팁 알.

답변

0

이것은 엉망입니다. '길이'를 책의 속성으로 지정하려면 길이를 책과의 관계에있는 별도의 모델로 지정해야합니다. 길이 모델을 가지고 있지 않습니다. Book 속성으로 'length'로 이동하십시오.

+0

저는 완전히 발전하기 때문에 다양한 각도에서 접근하려고합니다. 당신이 작성한 것을 바탕으로, 나는 : 1) 길이 모델 삭제. 2) attr_accessor : length를 Book.rb에 추가하십시오. 3) b.length = guide.length를 삭제하고 단순히 self로 길이를 호출하십시오. 내가 필요할 때 (groups = sorted_points.in_groups_of (self.length.count, false)와 같이) – rhjs

+0

1) yes; 2) 아니요. 마이그레이션에서 'legth'열을 추가 했으므로 attr_accessor가 필요하지 않습니다. 3) b.length = guide.length는 ClassMethods 'self'가 Book 인스턴스가 아니기 때문에 self.length를 호출 할 수 없습니다. – cthulhu

+0

그렇다면 b.length = guide.length를 남겨두면 'groups = sorted_points.in_groups_of (self.length.count, false)'가 sorted_points를 나누는 올바른 방법일까요? 마지막 질문은 길이 자체가 정수인 경우 self.length에 실제로 필요한 '개수'입니까? – rhjs