Forum: Ruby Sequel Newbie question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ruby N. (Guest)
on 2008-12-23 08:25
Hello Folks, I am trying out sequel and have a simple question.

Here is my sample:
DB = Sequel.connect('mysql://blah:blah@localhost:3306/thyu')

news_stories = DB[:feed_items].filter({:content_grabbed => '0'} &
(:url.like(/abcd.com/)))

puts news_stories.count
for story in news_stories
# logic to grab content
     if (content.length > 0)
         contentDoc = Hpricot(content).to_plain_text
         File.open("#{article_id}.txt", 'w') {|f| f.write(contentDoc) }
         puts article_id
         story.update(:content_grabbed => '1') # does seem to update ..
WHY?

         # Also how do I save another data to ANother table?
         # I get an sql error.
         puts "Did it update?"
         exit
     end
end

I am sure I am missing something basic here..Can u help pls?

Thanks!
Rubix
Jeremy E. (Guest)
on 2008-12-25 03:51
Ruby N. wrote:
> Hello Folks, I am trying out sequel and have a simple question.
>
> Here is my sample:
> DB = Sequel.connect('mysql://blah:blah@localhost:3306/thyu')
>
> news_stories = DB[:feed_items].filter({:content_grabbed => '0'} &
> (:url.like(/abcd.com/)))
>
> puts news_stories.count
> for story in news_stories
> # logic to grab content
>      if (content.length > 0)
>          contentDoc = Hpricot(content).to_plain_text
>          File.open("#{article_id}.txt", 'w') {|f| f.write(contentDoc) }
>          puts article_id
>          story.update(:content_grabbed => '1') # does seem to update ..

story is a Hash, not a Sequel::Model.  You are calling Hash#update,
which updates the Hash, but does not change the database.  What you want
to do is:

  # Filter to just the record, then update
  DB[:feed_items].filter(:id=>story[:id]).update(:content_grabbed =>
'1')

Or you could just use a model, which will do basically the same thing.

>          # Also how do I save another data to ANother table?

Just change the table you are using, and assuming the record would be
new in that table:

  DB[:feed_items2].insert(story)

>          # I get an sql error.

You need to post the error you are getting with a traceback in order to
provide adequate support.

>          puts "Did it update?"
>          exit
>      end
> end

Jeremy
This topic is locked and can not be replied to.