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]])
Related
I need to build a query to select records by two criteria (two models): Branch(id) and Plan(created_at)
I tried following queries:
#branches = Branch.where(id: 2).includes(:plans).where("created_at > ?", 1.week.ago)
#branches = Branch.where(id: 2).includes(:plans).where(plans: { "created_at > ?", 1.week.ago })
#branches = Branch.includes(:plans).where(branches: { id: 2 }, plans: { "created_at > ?", 1.week.ago })
I have an error
syntax error, unexpected '}', expecting keyword_end ... "created_at >
?", 1.week.ago })
How can I fix it?
Just from reading your code, the problem is with incorrect syntax of your hash:
{ "created_at > ?", 1.week.ago }
Which should be an array:
[ "created_at > ?", 1.week.ago ]
But for your use case, I think you need something like:
#branches = Branch.where(id: 2)
.joins(:plans)
.includes(:plans)
.where("plans.created_at > ?", 1.week.ago)
In order to be able to use a Plan's column created_at, includes is not enough - in that case the error will be thrown of unknown plans. This is why you need to specify joins to properly build whole query.
Hope that helps!
Good luck!
{ "created_at > ?", 1.week.ago } isn't correct hash syntax, it should be in an array:
[ 'created_at > ?', 1.week.ago ]
May this works for you,
Branch.includes(:plans).where(["id = ? and plans.created_at > ?", 2, 1.week.ago])
#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])
I'd like to group an array of ActiveRecord objects into a hash with an interface that's simple to query after an SQL statement that looks like the following:
SELECT name,value from foo where name IN ('bar', 'other_bar') LIMIT 2;
After that query, I want a hash where I can go:
foo[:bar] # output: value1
foo[:other_bar] # output: value2
What's the best way to collect the objects with ActiveRecord and group them so I can use the interface above?
In Rails 2
foos = Foo.all :select => "name, value",
:conditions => ["name in (?)", %w(bar other_bar)],
:limit => 2
In Rails 3
foos = Foo.where("name in (?)", %w(bar other_bar)).select("name, value").limit(2)
Then
foo = Hash[foos.map { |f| [f.name.to_sym, f.value] }]
or
foo = foos.inject({}) { |h, f| h[f.name.to_sym] = f.value; h }
or in Ruby 1.9
foo = foos.each_with_object({}) { |f, hash| hash[f.name.to_sym] = f.value }
If I understood you correctly:
foo = Hash[Foo.find(:all, :limit => 2, :select => "name, value", :conditions => ["name in ('bar', 'other_bar')"]).map { |s| [s.name.intern, s.value] }]
Hash[result.map { |r| [r[:name].to_sym, r[:value]] } ]
models.inject({}) {|h,m| h[ m.name.to_sym ] = m.value; h }
Is there a better way of writing this? Is it possible to do cleanly in one line?
conditions = ["category = ?", params[:category]] if params[:category]
#events = CalendarEvent.all( :conditions => conditions )
Not really too much to consolidate but you don't have a lot going on so shouldn't matter.
def action
options = {:conditions => ['category=?', params[:category]]} if params[:category]
#events = CalendarEvent.find(:all, options)
end
#events = CalendarEvent.all(
:conditions => (params[:category] ? ["category = ?", params[:category]] : nil))
<%= 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.