Ruby on Rails Tuesday, June 28, 2011

Hi all,

What I am trying to do:

1) Search for books at a specific school
2) Iterate through each book and find all its related subbooks where
the book state is of a specific type
3) Return an array of those subbooks for each book
4) Since the array will have subarrays of the subbooks, flatten them
into one big array
5) then return the subbook with the lowest sequence number

What I did:

subbook = Subbook.get_for_assignment(site, :available_for_assignment)


def self.get_for_assignment(site,book_state)

books = Book.where(:location_id => site, :location_type =>
'Site').all

available_books = books.each.map do |book|
book.subbooks.where(:book_state_id =>
BookState[:available_for_assignment].id)
end

raise ApplicationError, "Book out of order" unless
available_books.size > 0


available_book = available_books.flatten.map(&:sequence).min
#available_books.flatten.first (temporary although inaccurate fix)
if available_book.sequence > 100
available_book.book.update_attributes! :book_state_id =>
BookState[:distributed].id
end
available_book
end


The results:

"undefined method sequence for 1:Fixnum"


This line right here:

available_books.flatten.map(&:sequence).min

produces the above error. It's this part "map(&:sequence).min" that is
causing it. Basically I am flattening the array of subbooks and then
searching the one which has the lowest value for the sequence
attribute, corresponding to a sequence field in the subbooks table.

thanks for response

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

No comments:

Post a Comment