I have the following action in my controller:
def find_by_registration_date
#registration_date = params[:registration_date]
#registrations = Registration.where(:created_at => #registration_date)
end
...were params[:registration_date] is a simple date (no time) like:
"registration_date"=>"2014-07-16"
...and my created_at date looks like...
created_at: "2014-07-16 15:50:52"
How can a search based on Y-M-D?
If its mysql, you can do
#registrations = Registration.where("DATE(created_at) = ? ", #registration_date)
For other databases, find the equivalent date function
Related
Takes a query using some methods like code bellow. How can I ignore the nil values? For example: if the method date returns nil I wanna that the query use just array_one and array_to.
def array_one
...
end
def array_two
...
end
def date
...
end
Record.where(array_one: array_one, array_two: array_two, date: date)
You can chain your queries together (it's called lazy loading). They won't actually be executed until the first thing that calls query. This allows you to 'build' the query
query = Record.where(array_one: array_one, array_two: array_two)
query = query.where(date: date) if date.present?
query.each do |row| # now the query is executed
# do stuff
end
Record.where({ array_one: array_one, array_two: array_two, date: date }.compact)
I would do something like this:
scope = Record.scope
scope = scope.where(array_one: array_one) if array_one
scope = scope.where(array_two: array_two) if array_two
scope = scope.where(date: date) if date
scope
I am trying to create a query to record my submissions for this month in my controller using the following code:
#today = Time.now
#thismonth = #today.month
#nextmonth = #thismonth + 1
#submissions = SubmissionLocation.includes(:submission).where(location_id: current_agent.Company_Business_Location, location_id: 2134).map(&:submission)
#submissions_by_group = #submissions.group_by { |m| m.Max_move_in_date.beginning_of_month}.sort_by{:Max_move_in_date}
#submission_this_month = #submissions_by_group.where(:Max_move_in_date => #thismonth)
It seems to be throwing a nomethod error for the where condition in the #submission_this_month instance variable. I understand that it is most likely because my data is now in a hash format, so in that case, how I would I then query it?
I have several records for which title starts with:
Nick1
Nick2
Nick3
Othername1
How can I select all the records of which the title starts with "Nick" and have them rendered in the correct order? Something like:
#records = Record.where(title starts with: params[:title_name])
render json: #records
You can use the LIKE operator here:
#records = Record.where('title LIKE ?', "#{params[:title_name]}%").order(:title)
I would prefer to put these into a scope:
scope :title_search, ->(title){ where('title LIKE ?', "#{title}%") }
and call it via:
#records = Record.title_search(params[:title_name])
You can try Searchlogic .
Then it's as easy as:
#search = Record.new_search(params[:search])
#search.condition.title_starts_with = "Nick"
#models = #search.all
Or you can try
Record.where("title LIKE :prefix", prefix: "#{prefix}%")
I'd recommend using arel as in "How to do a LIKE query in Arel and Rails?"
records = Record.arel_table
Record.where records[:title].matches("#{params[:title_name]}%")
I've got some issues with writing my model scope. I want to filter my model objects based on a month in which they are published, i.e model BlogPost :
scope :published_in_month, ->(date) { where(published_date: date.at_beginning_of_month..date.at_end_of_month) }
So this is my controller method :
def list_by_month
#date = Time.parse("#{params[:year]}/#{params[:month]}")
puts "DATE IS #{#date}"
#posts = BlogPost.published_in_month(#date).page(params[:page]).per(10)
render :index
end
So date printed out I see is :
DATE IS 2013-12-01 00:00:00 +0100
But in my log and it the page I see post(s) from wrong month, this is a entry log :
SELECT "blog_posts".* FROM "blog_posts" WHERE ("blog_posts"."published_date" BETWEEN '2013-11-30 23:00:00.000000' AND '2013-12-31 22:59:59.999999') LIMIT 10 OFFSET 0
Where is this 2013-11-30 coming from when my input date is 2013-12-01, and how can I rewrite my scope if I made mistake with it to produce incorrect query
This could help:
scope :from_month, -> { where(created_at: DateTime.now.beginning_of_month..DateTime.now.end_of_month) }
Perhaps it is better to use SQL methods to determine the month of each record:
scope :published_in_month, ->(date) { where("MONTH(created_at) = ?", date.month) }
Postgresql version:
scope :published_in_month, ->(date) { where("DATE_PART('month', timestamp created_at) = ?", date.month) }
(not tested)
In my controller I'd like to do something like the following:
#book = Book.find(:all, :conditions = > [" created_at > with in the last 1 minute "]
if #book.nil?
# Just incase we didn't create a book, we'll initialize one
#book = Book.create()
end
#chapters = #book.chapters.build etc.............
* In sum, when the user is uploading a chapter, if they've recently created a book, I want the chapter to automatically go to that book and to make a new book.
Thoughts? thank you all
Hi Your code may be something like
time = Time.now
#book = Book.find(:all, :conditions = > [" created_at >= '#{Time.utc(time.year, time.month, time.day, time.hour, time.min - 1)}'"]) // .first if you're sure that it'll return just one record
if #book.blank? //.blank? not .nil? because the result of find is [] not nil
# Just incase we didn't create a book, we'll initialize one
#book = Array.new() //if you're sure that find'll return just one book you may don't change your code here
#book.first = Book.create()
end
//if you're sure that find'll return just one book you may don't change your code here
#book.each do |book|
#chapters = #book.chapters.build etc.............
end
if you're looking for a book created by some user you must pass user_id to this method and your conditions'll be
:conditions = > [" created_at >= '?' AND author_id = ?", Time.utc(time.year, time.month, time.day, time.hour, time.min - 1), params[:author_id]])