Ruby on Rails Friday, March 29, 2013

Hey, thanks for the answer.

As for the Book.rb - yes, it's a typing error.

When I run Book.first.length in the rails console, I get:

1.9.3-p327 :001 > Book.first.length
  Book Load (0.2ms)  SELECT "books".* FROM "books" WHERE "books"."label" = 'app LIMIT 1
 => nil

And yes, I did run the migration.

I think the problem can lie in Mongo collections. I've added the "length" collection by updating an already working Mongo Document. Maybe that's the problem?  I'll try to set up another mongo document with the length collection only.

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

W dniu czwartek, 28 marca 2013 16:52:13 UTC+1 użytkownik Colin Law napisał:

On 28 March 2013 13:07,  <maciej...@timeself.pl> wrote:
> Hey
>
> I have a problem with nomethoderror and have no idea how to solve it.
>
> In logs:
>
> NoMethodError (undefined method `length=' for #<Book:0x000000081583f0>):
> 2013-03-28T12:38:35+00:00 app[web.2]:   app/models/engine/book.rb:13:in
> `block in find_or_create_by_guide'
> 2013-03-28T12:38:35+00:00 app[web.2]:   app/models/engine/book.rb:9:in
> `find_or_create_by_guide'
> 2013-03-28T12:38:35+00:00 app[web.2]:
> app/controllers/guide_controller.rb:37:in
> `find_or_create_book_based_on_guide'
>
> cache: [GET /en/books/planner/preview] miss
> 2013-03-28T12:38:35+00:00 app[web.2]: Processing by BooksController#preview
> as */*
> 2013-03-28T12:38:35+00:00 app[web.2]:   Parameters: {"locale"=>"en",
> "id"=>"planner"}
> 2013-03-28T12:38:35+00:00 app[web.2]: Unknown label 'www'. Switching label
> to bookless
> 2013-03-28T12:38:35+00:00 app[web.2]: Finded book #515439c0e622d90002000019
> 2013-03-28T12:38:35+00:00 app[web.2]: Completed 500 Internal Server Error in
> 82ms
>
> Book.rb:
> module ClassMethods
>       def find_or_create_by_guide(guide)
>         book = ::Book.find_or_create_by_document(guide.id.to_s) do |t|
>           t.city_id = guide.city
>           t.host    = guide.host
>           t.pace    = guide.pace || :normal
>           t.length  = guide.length
>         end
> Later in Book.rb, I have:
>
> groups = sorted_points.in_groups_of(length, false)
>
>
> In Book.rb I have attr_accessible :length. I've also updated the table with
> length column:
> class AddLengthColumnToBooks < ActiveRecord::Migration
>   def change
>     add_column :books, :length, :integer
>   end
> end
>
> I have no idea what's going on. Can someone please help me with this?

The error refers to book.rb but you refer to Book.rb.  Is that just a
typing error?

What happens if, in the Rails Console you do
Book.first.length

I presume you have run the migration.  Have you checked using a
database tool that the column exists?

I wonder whether length is a reserved word.  If all else fails try a
different name.

Colin

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/3KVs8mswjlkJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments:

Post a Comment