Ruby on Rails Ransack Datetime to date search
Asked Answered
P

3

8

Like carvil I have in my model a datetime for created_at although I wanted the "equals" predicate to compare the created_at and a date (like '2012-09-26').

So I added in my model (in order to add casted attributes and take off the old created_at/update_at/deleted_at :

ransacker :created_at do
    Arel::Nodes::SqlLiteral.new("date(items.created_at)")
end

ransacker :updated_at do
    Arel::Nodes::SqlLiteral.new("date(items.updated_at)")
end

ransacker :deleted_at do
    Arel::Nodes::SqlLiteral.new("date(items.deleted_at)")
end

# Hide some attributes for advanced search
UNRANSACKABLE_ATTRIBUTES = ["created_at", "updated_at", "deleted_at"]

def self.ransackable_attributes auth_object = nil
    (column_names - UNRANSACKABLE_ATTRIBUTES) + _ransackers.keys
end

But when I confirm the query (created_at equals to '2012-03-24') I have this error:

NoMethodError (undefined method `name' for "date(items.created_at)":Arel::Nodes::SqlLiteral):

Surprisingly, it works with "greater than" and "less than". Only "equals" occurs this error.

I made all of this for all my models and 60% works (the remain 40% occurs this error).

In the console :

irb(main):232:0> Item.search(:created_at_eq => Date.today.to_s).result
(Object doesn't support #inspect)

Thanks for your help

EDIT :

I have a default_scope which makes : Item(:deleted_at false)

But I don't know why it occurs the error

Putup answered 26/9, 2012 at 15:43 Comment(1)
It looks like this hasn't been resolved yet: github.com/activerecord-hackery/ransack/issues/34. Did you ever get your issue figured out?Syncretize
R
4

Refer to second example of Ransack wiki,

in the model:

ransacker :created_at , type: :date do
  Arel.sql('date(created_at)')
end

in the view:

<%= f.search_field(
  :created_at_date_equals, placeholder: t(:date_format)
  ) %>
...
<%= sort_link(@search, :created_at, default_order: :desc) %>

config/initializers/ransack.rb

Ransack.configure do |config|
  config.add_predicate 'date_equals',
  arel_predicate: 'eq',
  formatter: proc { |v| v.to_date }
  validator: proc { |v| v.present? },
  type: :string
end

The new predicate 'date_equals' is added for the date equal search. The ransacker declared with type: :date and get the date from datetime column with Arel

Rechabite answered 10/12, 2018 at 16:0 Comment(0)
F
2

I had some problems with this, and came with this:

ransacker :created_at, type: :date do
  Arel.sql('date(created_at)')
end
Favian answered 9/10, 2017 at 19:14 Comment(0)
B
1

Please changes Arel::Nodes::SqlLiteral.new('date(column_name)') with Arel.sql('date(column_name)').

Your syntax will be ::

ransacker :created_at do
    Arel.sql("date(items.created_at)")
end

ransacker :updated_at do
    Arel.sql("date(items.updated_at)")
end

ransacker :deleted_at do
    Arel.sql("date(items.deleted_at)")
end

I hove this would be helpful.

Beautiful answered 18/12, 2015 at 15:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.