Why does Sequel return only the first matching row? - ruby

I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. The code below gives me only the first matching row instead of all matching rows, of which there should be many. It doesnt matter what I use inside where(...) it only ever gives me the first matching row. If I use schedule.all it gives me the entire database but only the first matching row if I use where.
Where am I going wrong?
.rb
require 'date'
require 'sequel'
require 'sinatra'
DB = Sequel.connect("sqlite://sk.db")
class Sked < Sequel::Model
end
schedule = DB.from(:sked)
get '/' do
#todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= #todaymatches.inspect %></p>

.where(...) queries don't actually retrieve records, they return datasets that can be used to chain more queries for example:
my_posts = DB[:posts].where(:author => 'david').where(:topic => 'ruby') # no records are retrieved
If you want to actually retrieve the records, put an all at the end:
#todaymatches = schedule.where(:date => Date.today).all # records are retrieved
See: https://sequel.jeremyevans.net/rdoc/classes/Sequel/Dataset.html

This might be clunky, but I think it will do what you want. Give it a whirl.
.rb
.
.
.
get '/' do
#todaymatches = schedule.where(:date => Date.today)
def print_today_matches
#todaymatches.each { |x| puts x.inspect }
end
erb :games
end
.erb
<h1>Games</h1>
<p><%= print_today_matches %></p>
Or, alternatively:
.rb
.
.
.
get '/' do
#todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= #todaymatches.each { |x| p x } %></p>

Related

How to display results from database in Sinatra using Sequel?

I have an SQLite3 database called sk.db with a table called Sked that displays a schedule of sports matches with a column date. I am simply trying to display today's matches. It appears as though the connection to the database is not working, though I do not get any errors.
I have tried looking through the Sequel documentation to no avail. How can I display results from an existing database in Sinatra?
.rb
require 'date'
require 'sequel'
require 'sinatra'
DB = Sequel.connect("sqlite://sk.db")
class Sked < Sequel::Model
end
schedule = DB.from(:sked)
get '/' do
todaymatches = schedule.where(:date => Date.today)
erb :games
end
.erb
<h1>Games</h1>
<p><%= #todaymatches %></p>
.where doesn't actually retrieve data, but instead returns a dataset. Add an .all to actually retrieve the data
todaymatches = schedule.where(:date => Date.today).all

Rails 4 search bar

I'm trying to create a search bar in my Rails 4 app. I'm my user db has 'name' and 'email' columns for the user's - I want users to be able to search for other users by name or id.
I'm currently getting this:
ActiveRecord::RecordNotFound in UsersController#index
Couldn't find all Users with 'id': (all, {:conditions=>["name LIKE ?", "%hi#example.com%"]}) (found 0 results, but was looking for 2)
Does anyone know what I'm doing wrong? I've looked at railscasts and a few forums etc but cant get past this point at the moment.
index.html.erb:
<% form_tag users_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
model/user.rb:
def self.search(search)
if search
find(:all, :conditions => ['name LIKE ?', "%#{search}%"])
else
find(:all)
end
end
users_controller.rb:
def index
#users = User.search(params[:search])
end
routes.rb:
get 'search' => 'users#index'
Are you using Rails 4? find(:all, ...) is the old way of doing things. find only takes ids now. Use:
def self.search(search)
if search.present?
where('name LIKE ?', "%#{search}%")
else
where(true)
end
end
Also present? will test against both nil and blank. Keep in mind that LIKE can be really slow depending on your database.
Searching in rails
By default rails doesn't support full text searching .Activerecord finder always find a record using the primary key ie. id.
Hence , we need some other gems or applications like sunspot, elasticsearch etc..
i'll show you using Sunspot solr here ..
in the gem file of your application just add the following code...
.
gem 'sunspot_rails'
group :development do
gem 'sunspot_solr'
end
and in terminal use
bundle install
rails g sunspot_rails:install
this will add solr and creates config/sunspot.yml file
rake sunspot:solr:start
rake sunspot:reindex
edit the user.rb lile in models
app/model/user.rb
Class User < ActiveRecord::Base
searchable do
text :name, :email
end
end
in app/controller/users_controller.rb
and then add in index function
def index
#search = User.search do
fulltext params[:search]
end
#users = search.results
end
Make a form to take a user input
<%= label_tag(:search, "Search for:") %>
<%= text_field_tag(:search) %>
<%= submit_tag("Search") %>
<% end %>

How to display contents from a sequel database connection in the view file in Ruby?

I am trying to retrieve data from a PostgreSQL database with Sequel in Sinatra.
DB = Sequel.connect('postgres://connection_data')
items = DB[:items]
Then I try to get an entry with a specific ID:
get '/:id' do
#item = items.filter(:id => params[:id])
erb :edit
end
In my edit view I would like to display the content of the #item variable. The problem is that I don´t know how to get for example the ID.
<% if #item %>
Do something
<% else %>
<p>Item not found.</p>
<% end %>
I tried using #item.id and #item[:id] but both don´t work. I get an error undefined method 'id' for #<Sequel::Postgres::Dataset:0x007fac118b7120>. What would be the right way to retrieve the values from the #item variable?
#item = items.filter(:id => params[:id]) returns a dataset. If you want a single item, you should do: #item = items.first(:id => params[:id].to_i)
Also #item.id is probably not want you want. Given that items = DB[:items], you are using a plain dataset and then #item = items.first(:id => params[:id].to_i) is going to give you a hash. You need to do #item[:id] to get the item's id.
You may want to look at using models instead:
# model file
class Item < Sequel::Model; end
# sinatra code
#item = Item[params[:id].to_i]
# template
#item.id
Actually #item.id is the right way. The only problem I can see in your code is
#item = items.filter(:id == params[:id])
which should be
#item = items.filter(:id => params[:id].to_i)
EDIT:
Try this:
#item = items.where(:id => params[:id].to_i)
#item.select(:id) #to embed
params[:id] is giving a string, so convert it to an integer.

How to see paging results with Sequel and Sinatra

I am using will_paginate and attempting to page a dataset from Sequel. The requires are:
require 'will_paginate'
require 'will_paginate/sequel'
require 'will_paginate/collection'
require 'will_paginate/version'
require 'sequel/extensions/pagination'
The Ruby code is:
get '/candidate' do
#items = DB[:candidates].order(:id).extension(:pagination).paginate(1, 10)
erb :candidate
end
In the view: <%= will_paginate #items %>
The dataset renders correctly with 10 records and when I click "2" or "Next" the address in the browser changes to http://localhost:4567/candidate?page=2 but the records remain the same. Effectively, the results are not paged and I cannot get past page 1.
The numbers for the page and the number of records are hardcoded in your example using paginate(1, 10), so it will always bring back page 1 with 10 records. You need to pass on the page=2 parameter from the query-string. This is done via the params helper:
get '/candidate' do
#items = DB[:candidates].order(:id).paginate(:page => params["page"].to_i, :per_page => 10)
erb :candidate
end
If you wanted, you could also pass on the per_page in the query-string by adding this code:
get '/candidate' do
#items = DB[:candidates].order(:id).paginate(:page => params["page"].to_i, :per_page => params["per_page"].to_i)
erb :candidate
end
I'd add a default for both in case they're not given. I understand you can do this via the library, e.g. WillPaginate.per_page = 10, but you could also do this in the route, via:
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = DB[:candidates].order(:id).paginate(:page => page.to_i, :per_page => per_page.to_i)
erb :candidate
end
I didn't notice before you were also using the Sinatra helper provided by will_paginate.
I'd either call paginate on the dataset or get a dataset, unpaginated, and pass it to the helper. So either this:
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = DB[:candidates].order(:id).paginate(:page => page.to_i, :per_page => per_page.to_i)
erb :candidate
end
# in the view
<%= #items %>
or this:
get '/candidate' do
#items = DB[:candidates].order(:id)
erb :candidate
end
# in the view
<%= will_paginate #items, params %>
So, from what I can see, the Sequel paginate method is not overwritten or wrapped/overloaded, so its method signature is the same as it would be if you were just using Sequel and not will_paginate as well. Which means that this code worked for me:
require 'will_paginate'
require 'will_paginate/sequel'
get '/candidate' do
page = params.fetch "page", 1
per_page = params.fetch "per_page", 10
#items = Repo.db[:candidates].order(:id).paginate(page.to_i, per_page.to_i)
haml :candidate
end
In the Haml view:
- #items.each do |i|
= i[:title]
Since the method signature is the same I'm not sure what advantage you gain from using will_paginate over Sequel's paginate at all. I couldn't find a way to get the Sinatra helper to work.

Why can I not save tweets into the database?

I want to save the tweets into the database, but it doesn't work! I'm using the twitter gem.
My controller:
class TweetsController < ApplicationController
def save_tweets_into_database
#tweets = Twitter.search("#em2012" "#Italy" "#Spain", :lang => "en", :rpp => 25).results
#tweets.each do |tweet|
#tweet = tweet
#tweet.create
end
end
end
My Model
require "rubygems"
require "twitter"
class Tweet < ActiveRecord::Base
end
My view
<% #title = "liste" %>
<h2>Liste der Tweets über das EM-Finale</h2>
<ul>
<% #tweets.each do |tweet| %>
<li><%= tweet %></li>
<% end %>
</ul>
my routes
Em2012tweet::Application.routes.draw do
match '/save_tweets', to: 'tweets#save_tweets_into_database'
end
This error is displayed:
undefined method `create' for Twitter::Status:0x007fac9c80a160
Rails.root: /Users/xyz/Desktop/Workspace/em2012tweet
app/controllers/tweets_controller.rb:7:in `block in save_tweets_into_database'
app/controllers/tweets_controller.rb:5:in `each'
app/controllers/tweets_controller.rb:5:in `save_tweets_into_database'
save the tweets into the database
In Rails, following REST convention, index action is associated with simply displaying all the objects of a model already stored in database. And this is actually what you did with the code as well. Your index action isn't doing anything that will save the tweets into the database.
I don't think you are trying to save tweets here. You are trying to simply display them. Try this if you want to simply display tweets in index view file.
def index
#tweets = Twitter.search("#em2012" "#italy" "#spain", :lang => "en", :rpp => 25).results
end
If you want to save tweets, create an action like this.
def save_tweets_into_database
#tweets = Twitter.search("#em2012" "#italy" "#spain", :lang => "en", :rpp => 25).results
#tweets.each do |tweet|
Tweet.save(tweet)
end
end
tweets_controller.rb:
class TweetsController < ApplicationController
def save_tweets_into_database
#tweets = Twitter.search("#em2012" "#Italy" "#Spain", :lang => "en", :rpp => 25)
#tweets.each do |tweet|
Tweet.create(twitter_tweet: tweet)
end
end
end
tweet.rb:
class Tweet < ActiveRecord::Base
serialize :twitter_tweet
end
Note that this requires you have a migration where a tweets table is created with a text column named 'twitter_tweet', e.g.
class CreateTweets < ActiveRecord::Migration
def change
create_table :tweets do |t|
t.text :twitter_tweets
t.timestamps
end
end
end
This will allow you to call save_tweets_into_database and save the 25 returned tweets into the database. This isn't a very good strategy, as you are serializing each of the Twitter::Status objects and storing them as YAML in your database. This means you lose the ability to use all the nice ActiveRecord helpers, and instead must deserialize all of the objects you want to work with before actually using them. A better strategy would be to create an ActiveRecord object that has the same attributes as those in the Twitter::Status object you wish to save. Then you can map the fetched Twitter::Status objects to the new ActiveRecord Tweet objects and retain all the benefits of using ActiveRecord.
I'll not say anything about your routing structure, other than it definitely doesn't appear to be following the 'Rails' way, which, from experience, typically leads to quite a few headaches.

Resources