After the correction of a wrong syntax I have a new error and I am not sure where it comes from:
categories were created in the console
Category Load (2.0ms) SELECT "categories".* FROM "categories"
=> #<ActiveRecord::Relation [#<Category id: 1, name: "Ruby", created_at: "2016-09-26 09:03:17", updated_at: "2016-09-26 09:03:17">, #<Category id: 2, name: "Rails4", created_at: "2016-09-26 09:03:25", updated_at: "2016-09-27 14:32:39">, #<Category id: 3, name: "Rails5", created_at: "2016-09-26 09:03:30", updated_at: "2016-09-27 14:35:25">, #<Category id: 4, name: "Heroku", created_at: "2016-09-26 09:03:35", updated_at: "2016-09-27 14:35:47">, #<Category id: 5, name: "AWS-Amazon", created_at: "2016-09-26 09:03:43", updated_at: "2016-09-26 09:03:43">]>
When I select:
- "Ruby" I have:
malformed format string - %R
- "Rails4" I have:
malformed format string - %R
- "Rails5" I have:
malformed format string - %R
- "Heroku" I have:
malformed format string - %H
- "AWS-Amazon" I have:
too few arguments
I am trying to filter my categories by name:
category model
class Category < ActiveRecord::Base
has_many :tutos
def self.filter(filter)
if filter
where(["name LIKE '%#{filter}%'"]).order('created_at DESC')
else
all
end
end
end
1st Edit
I've tried different way but I have issues in each:
`where(["name LIKE '%#{filter}%'"]).order('created_at DESC')`
returns the malformed string alert
`where("name LIKE '%#{filter}%'").order(created_at: :desc)`
and
`where("name LIKE ?", "%#{filter}%").order('created_at DESC')`
return all the categories without filtering....
you may want to see the tutos/views/index
.container
.row
h1.text-gray Tutorials
.row.search_banner
.col-xs-3
=form_tag tutos_path, :method => 'get' do
=text_field_tag :search, params[:search], placeholder:"Search by keywords"
=submit_tag "Search", class:'btn btn-xs btn-default btn-search'
.col-xs-3
=form_tag tutos_path, :method => 'get' do
=select_tag :filter, options_for_select(Category.all.map{|c| c.name}, params[:filter])
=submit_tag "Search", class:"btn btn-xs btn-default btn-search"
.col-xs-3
-if user_signed_in?
= link_to "Create a tuto", new_tuto_path, class:"btn btn-success"
br
br
#tutos.transitions-enabled
-@tutos.each do |tuto|
.box.panel-default
= link_to image_tag(image_by_category(tuto.category.try(:name))), tuto_path(tuto)
h3 = link_to tuto.title, tuto_path(tuto), class:"title-link"
h6
| Created by:
span<>
= tuto.user.full_name
br
span.glyphicon.glyphicon-heart
span<>
= tuto.get_upvotes.size
br
br
2nd edit
Rails console log : if I search for "Heroku" Cartegory: but as my categories belongs to tutos... (Maybe have a look on the website filter are not online yet though but you will see the idea)
Should I do something like this
Tuto.category.where("name LIKE ?", "%#{filter}%").order('created_at DESC')
Started GET "/tutos?utf8=%E2%9C%93&filter=Heroku&commit=Search" for ::1 at 2016-09-28 17:50:41 +0200
Processing by TutosController#index as HTML
Parameters: {"utf8"=>"✓", "filter"=>"Heroku", "commit"=>"Search"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Tuto Load (0.1ms) SELECT "tutos".* FROM "tutos"
User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" IN (1)
Category Load (0.1ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (2, 3, 5, 4, 1)
(0.1ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 16], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.1ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 17], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 18], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 19], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 20], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 21], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 22], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 23], ["votable_type", "Tuto"], ["vote_flag", "t"]]
(0.0ms) SELECT COUNT(*) FROM "votes" WHERE "votes"."votable_id" = ? AND "votes"."votable_type" = ? AND "votes"."vote_flag" = ? AND "votes"."vote_scope" IS NULL [["votable_id", 24], ["votable_type", "Tuto"], ["vote_flag", "t"]]
Rendered tutos/index.html.slim within layouts/application (29.0ms)
Rendered layouts/_nav.html.slim (0.4ms)
Completed 200 OK in 104ms (Views: 94.1ms | ActiveRecord: 1.9ms)
3rd Edit
tuto model
class Tuto < ActiveRecord::Base
acts_as_votable
belongs_to :user
belongs_to :category
validates :category_id, presence: true
def self.search(search)
if search
where(["title LIKE ?","%#{search}%"]).order('created_at DESC')
else
all
end
end
end
category model
I've build my category filter
like the tuto search
class Category < ActiveRecord::Base
has_many :tutos
def self.filter(filter)
if filter
#where(["name LIKE '%#{filter}%'"]).order('created_at DESC')
#where("name LIKE '%#{filter}%'").order(created_at: :desc)
where("name LIKE ?", "%#{filter}%").order('created_at DESC')
else
all
end
end
end
Checked with the suggested raise 'check'
Feel free to ask for more code if needed, as I am not sure where I am wrong... Many thanks regards