If you *must* split it up in the database, your changing your mind
from 500 to 300 is going to suck, otherwise you might use a "pages"
assocation or something of the like which would be very simple...
for instance:
class Article < ActiveRecord::Base
has_many :pages
validates_presence_of :text
after_create
i = 0
b = text.scan(/\b\S+\b/)
b.each_slice(500) do |x|
self.pages.create(:page => i+=1, :text => x.join(" "))
end
end
end
class Page < ActiveRecord::Base
belongs_to :article
end
Someone probably has a MUCH prettier method of doing this, was just
kind of on-the-fly...
Cheers!
On May 31, 9:14 pm, Jian Lin <[email protected]> wrote:
> Jonathan Rochkind wrote:
> > Jian Lin wrote:
> >> I wonder if a Ruby on Rails developer has encounter this before: suppose
> >> it is a long article (say 100,000 words), and I need to write a Ruby
> >> file to display page 1, 2, or page 38 of the article, by
>
> >> display.html.erb?page=38
>
> >> but the number of words for each page can change over time (for example,
> >> right now if it is 500 words per page, but next month, we can change it
> >> to 300 words per page easily
>
> > Why divide it in the database? Store it one field in the database, and
> > when you fetch it from the database just perform the logic to find
> > page=38 and then display that.
>
> is it true that it all the 100,000 words are in one record (one row),
> then every time, the whole field needs to be retrieved. If we assume
> one work is about 6 characters long (with the space), then it is
> 600kbyte per read. I hope to make it "read as needed"... 500 words and
> about 3kbyte read per page each time.
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---