I'd like to ask, say for example this is my model validation in ruby on rails:
validates_uniqueness_of :name, conditions: -> { where("date = ?", some date from stimulus js) }
and the date will be obtained from a calendar through stimulus js:
let calDateHeader = document.querySelector(".ec-title").innerText
How is it possible to pass that date value so I could check that any record created is unique per date selected from my calendar.
Related
I have a model with attributes start_date and end_date. I have search form where user will put the date and I should get a data from the model if date is in between start_date and end_date.
how should I create a query with thinking sphinx.
You will need to do something like the following:
Add both start_date and end_date as attributes (not fields) to your model's Sphinx index.
Translate form params into a date or time value
Use range filters to limit search queries.
I've opted for very large windows of time, but essentially this ensures the given date is equal to or larger than the start date and less than or equal to the end date.
beginning, ending = Time.utc(1970), Time.utc(2030)
Model.search :with => {
:start_date => beginning..date_from_params,
:end_date => date_from_params..ending
}
I need to create a custom ActiveAdmin filter for a Date Range which belongs to another resource. I want to filter a list of users by a date range of when they completed a survey.
I've declared a custom filter in the ActiveAdmin DSL like so:
filter :by_date_completed, label: 'By Date Completed', as: :date_range
This makes me a nice date range in active admin to restrict my users by. All good.
I haven't been able to find much documentation on how to do this but in my user model I've tried to create a ransacker to handle the filter.
ransacker :by_date_completed, {
formatter: proc { |start_date, end_date|
time_range = start_date..end_date
users = User.joins(:surveys).where(surveys: { updated_at: time_range})
users = users.map(&:id)
users.present? ? users : nil
},
callable: proc { |parent|
parent.table[:id]
}
}
But ActiveAdmin passes the date ranges to the filter one at a time and so I can't get a range to search by?
What am I meant to do in this scenario? Am I going about the whole problem in the wrong way?
I think you don't need a custom ransacker for that. You can filter a cross associations.
This should work for you:
filter :surveys_updated_at, label: 'By Date Completed', as: :date_range
What you do is not, how a ransacker should work. I know it's a common google result, but it's wrong.
The ransacker don't receive both dates at once in ActiveAdmin. The ransacker will called with a by_date_completed_gteq and a by_date_completed_lteq.
A ransacker formatter is only to format the input value. Convert a String into a Int for example.
The block / the callable proc of the ransacker needs to return a Arel / SQL string which is placed in the DB query.
A abstract ransacker example:
ransacker :foo do
"ransacker sql code"
end
"SELECT * FROM bar WHERE 'ransacker sql code' = <input>"
Your ransack should look like this:
ransacker :by_date_completed do
"surveys.updated_at"
end
This should end up in a query like:
"SELECT * FROM users WHERE surveys.updated_at >= START_DATE AND surveys.updated_at <= END_DATE"
I have a model called submission and currently I am displaying all the records in the database in the index page. In the submissions controller I have :
#submissions = Submission.all
However, now I only want the record for the past two days or the name field equals to some string, I tried this but it still shows all the records for me:
#submissions = Submission.all(:conditions => ["updated_at >= ? OR name = ?", 2.days.ago.to_date, "me"])
where updated_at and name are two fields in the submissions table.
Any idea where is wrong?
EDIT:
In my submission model:
attr_accessible :name, :updated_at
In Arel (Rails 3), your conditions should be in a where method, with (optionally) at the end, like so
Submission.where("updated_at >= ? OR name = ?", 2.days.ago.to_date, "me").all
Query below works fine when user selects different dates but when user selects same dates like 3/5/2011 and 3/5/2011 it returns nothings. How can i handle this ? If user selects same dates, i want it to find clients which are created at that date.
Client.where(:created_at => date_from..date_to)
You may need to modify your query as below to get between beginning_of_day and end_of_day
Client.where(:created_at => date_from.beginning_of_day..date_to.end_of_day)
You could create a helper method:
def date_range(from, to)
from == to ? from : from..to
end
Client.where(:created_at=>date_range(date_from,date_to))
I am trying to chain a calendaer_date_select to a select field, so the select list is filtered by the choosen date. I have followed the instructions as described here
I have in the activescaffold config:
config.columns[:order_date].form_ui = :calendar_date_select
config.columns[:order_date].options = {:update_column => :sale}
config.columns[:sale].form_ui = :select
... and in the helper:
def options_for_association_conditions(association)
if association.name == :sale
{'sales.order_date' => #record.order_date}
else
super
end
end
The problem is that picking a date from the javascript widget thingy
doesn't trigger the select to refresh. However if I type in the date
then it does. Any ideas?
This was a bug with ActiveScaffold that was fixed this morning. So cloning the repository again will solve your problems.
For the record, the method ActiveScaffold uses to watch for changes doesn't catch the way that Calendar Date Select sets the field. ActiveScaffold watches for change events on fields for column updates. Change events are triggered by a modification in a fields value between the time it gains and loses focus. Calendar Date Select modifies the value without giving or removing focus to the field.
If you don't feel like updating your plugins, you could hack it together your self by doing the following:
config.columns[:order_date].options = {:update_column => :sale}
to
config.columns[:order_date].options = {:update_column => :sale,
:before_show => 'this.focus()', :onchange => 'this.blur()'}