Rails condition where days ago? - ruby-on-rails

#messages = current_user.message_participantions.where(:read => false, :updated_at > 5.days.ago)
The updated_at 5 days ago errors. Do I need to use a format like so:
find(:all, :conditions => ["created_at > ?", 5.days.ago])

You could do:
#messages = current_user.message_participantions.where("read = ? AND updated_at > ?", false, 5.days.ago)
Or if for some reason you need to use the hash:
#messages = current_user.message_participantions.where(:read => false, :updated_at => 5.days.ago..Time.now)
As the values of the hash argument to the where method can be exact values or ranges: http://api.rubyonrails.org/classes/ActiveRecord/Base.html

#messages = current_user.message_participantions.where(:read => false).where("updated_at > ?", 5.days.ago)

I would:
#messages = current_user.message_participantions.where(["read = 0 AND updated_at > ?", 5.days.ago])

Related

will_paginate for group query with sunspot 2.2.2 in rails

I am using sunspot 2.2.2 in my rails app for searching results,
I have this code for grouping in my model:
def self.search_products(params, currency = nil, page_uri = nil)
search_products = Sunspot.search(VariantPrice) do
group :code do
if params[:gallery_order].present?
order_by :price, params[:gallery_order].downcase.to_sym
elsif params[:new_arrival].present? || params[:name] == "new-arrivals"
order_by :product_created_at, :desc
else
if params[:fashion_type] == "fashion"
order_by :price, :asc
elsif params[:sort] != "lowhigh"
order_by :price, :asc
else
order_by :price, :asc
end
end
limit 1
end
end
and I have this code in my controller :
variant_prices = Product.search_products(params, #currency, request.original_fullpath)
#variant_prices = []
variant_prices.group(:code).groups.each do |group|
group.results.each do |result|
#variant_prices << result
end
end
#variant_prices = #variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
#variant_prices_count = variant_prices.group(:code).total
now I am getting the expected count that is #variant_prices_count, which is 1400 in my case, but I am getting #variant_prices count as 60 which is wrong in my case , here I was expecting to get 1400.and then I want to paginate with this result. cant understand whether it's will_paginate issue or something else.
Help!
You can get 1400 from the paginate instance also by Total entries
by this way replace count with total_entries
#variant_prices = #variant_prices.paginate(:page => params[:page], :per_page => PER_PAGE_VALUE)
#variant_prices.total_entries#it will return toal entries 1400

How to add a simple drop down in rails for pages

I used will_paginate to paginate, now I want to control how many records in one page.
I do know how to interact data from the select_tag. please tell me how to return the data to
#page in controller.
I used
<td><%= select_tag "count", "<option>10</option> <option>20</option>".html_safe%></td>
Controller
def index
#page = 10;
#users = User.order(:username).joins(:biography).where("`is_active?` = true AND `last_sign_in_at` > DATE_SUB(NOW(), INTERVAL 6 MONTH) ").paginate(:page => params[:page], :per_page => #page)
end
You can use the params[:count] like this to get the number of records per page based on the value selected in the select_tag
def index
#page = params[:count] #here
#users = User.order(:username).joins(:biography).where("is_active? = true AND last_sign_in_at > DATE_SUB(NOW(), INTERVAL 6 MONTH) ").paginate(:page => params[:page], :per_page => #page)
end

Rails Controller - Time , Condition and Geocoder Gem

I am struggling to amalgamate this two statements. I am working on an event app. I would like to block my users from publishing the events themselves. In order to do that i have a field in my table called "State" and i am also using the geocoder gem.
def index
if params[:search].present?
#events = Event.near(params[:search], 50, :order => :distance).where(:state => ['3', '4'])
else
#events = Event.where('until > ?', Time.zone.now)
end
end
I would like to do this....
def index
if params[:search].present?
#events = Event.near(params[:search], 50, :order => :distance).where(:state => ['3', '4'])
else
#events = Event.where('until > ?', Time.zone.now, :state => ['3', '4'])
end
end
but i get an error
ActiveRecord::PreparedStatementInvalid in EventsController#index
wrong number of bind variables (2 for 1) in: until > ?
Can you please help.....
You have only one placeholder '?', but you supply two arguments. You could do:
#events = Event.where('until > :time and state in :state', {:time => Time.zone.now, :state => ['3', '4']})
or
#events = Event.where('until > ?', Time.zone.now).where(:state => ['3', '4'])
or
#events = Event.where(:until.gt => Time.zone.now, :state => ['3', '4'])

check_box_tag and find condition

<%= check_box_tag('videos_count')%>
If this box is checked, the param will say "videos_count"=>"1" . In the controller I have this:
videos_count = params[:videos_count]
#cars = Car.paginate( :page => params[:page], :per_page => 10,
:conditions => ["videos_count = ?", videos_count],
when the box is checked I see the correct parameter in the server log, but the find returns all of the results instead of results with videos_count = 1.
Check the datatype of 'videos_count' if it's Tiny INT then following may worh. No checked though.
:conditions => ["videos_count = ?", true]
What will the output for this?
:conditions => ["videos_count = ?", 1])
I think there is an issue with your table.

Rails syntax error

Where is the error in this I can't see it:
news = News.find(:all, :conditions => [":simulation_id => session[:sim_id] AND :created_at > session[:last_login]"])
Try this:
news = News.find(:all, :conditions => ["simulation_id = ? AND created_at > ?", session[:sim_id], session[:last_login]])
Your conditions string won't be evaluated as you expect:
[":simulation_id => session[:sim_id] AND :created_at > session[:last_login]"]
change that to
["simulation_id = ? AND created_at > ?", session[:sim_id], session[:last_login]]
You can also call Model.all instead of Model.find(:all) which would look something like this:
news = News.all(:conditions => ["simulation_id = ? AND created_at > ?", session[:sim_id], session[:last_login]])

Resources