RoR Search conditions comparing two columns - ruby-on-rails-3

What I basically want to do is the following:
a = Assignment.all(:conditions => { :end_date < :start_date })
I only want to select those records on which the end_date is before the start_date.
I actually don't want ending up writing a
for each, push to array if end_date is earlier than start_date.
How can I achieve this in a pretty 'Railsy'-way?
Thank you in advance.
Edit:
I think the problem is comparing the values of both columns. (Allmost) every query is comparing a cell-value to an input-value.
This is a shot in the dark, but maybe one in the right direction ?
Haven't figured out a solution.

It's been a while here but nevertheless, you can just use the ArelTable method:
t = Assignment.arel_table
Assignment.where(t[:end_date].lt(t[:start_date]))
The condition predicates documentation: http://www.rubydoc.info/github/rails/arel/Arel/Predications
The ArelTable documentation: http://www.rubydoc.info/github/rails/arel/Arel/Table
And a good guide: http://jpospisil.com/2014/06/16/the-definitive-guide-to-arel-the-sql-manager-for-ruby.html

a = Assignment.all(:conditions => ["end_date < ?", :start_date ])
Check this: http://api.rubyonrails.org/classes/ActiveRecord/FinderMethods.html#method-i-all
I am assuming that you are using ActiveRecord

try
a = Assignment.where('regioassignments.end_date < regioassignments.start_date')
use the tablename followed by the EXACT COLUMNNAMES in the database, since ActiveRecord recognizes this and uses this as SQL directly.
That means the columnname for end_date is probably assignment_expected_end_date from what I figured from one of your comments.
adapted from this answer

Related

How check range beetwen to dates and make comparison in sql on ruby?

.joins(:residence) .where('(custom_services.date_from - ?) <= 1', Date.today)
Does anyone know how to correctly implement a check the difference between the date in custom_services.date_from and today's is less than or equal to one day(in the screenshot, one of the attempts is not correct)
Using Date.tomorrow and beginless range:
.where(date_from: ..Date.tomorrow)
if need to specify table name
.where('custom_services.date_from': ..Date.tomorrow)
you can just use
.where('custom_services.date_from <= ?', Date.today + 1.day)
Can you use something like this?
where("DATEDIFF(custom_services.date_from, ?) <= 1", Date.today)

Selecting entries whose `date_field < NOW()`

When I try to run the following query, it returns nothing:
Item::where(\DB::raw('date_field < NOW()'))->get()
The reason for this is, that is null is appended to the generated MySQL query like this:
SELECT * FROM items WHERE date_field < NOW() is null;
Why does the is null part get appended to the above query?
This is a known issue in Laravel and has been reported on their GitHub page. Use whereRaw() instead and pass a string:
Item::whereRaw('date_field < NOW()')->get()
No idea why the not null part gets appended. But I found a workaround.
Try this
Item::whereNotNull(\DB::raw('date_field < NOW()'))->get()
Of course, you may use built-in features like Carbon
Item::where('date_field', '<', Carbon\Carbon::now())->get()
Use it this way..
Item::where('date_field','<','NOW()')->get()
SELECT * FROM items WHERE date_field < GETDATE();

Need help converting SQL query to Ruby.

I'm new to Ruby on Rails. I'm trying to determine the proper ruby query for the following SQL query.
Select max(bid_amount) from biddings where listing_id = 1;
I need to extract the maximum value in the bid_amount column. But it has to have a dynamic listing_id.
Try:
Bidding.where('listing_id = :listing_id', listing_id: 1).maximum(:bid_amount)
Update:
To follow up on your comment: since you say you are passing in params[:id], it's best to convert that parameter to integer so that unwanted values don't go to the database. For e.g.
Bidding.where('listing_id = :listing_id', listing_id: params[:id].to_i).maximum(:bid_amount)

Using Sequel, can I create UPDATE statements with a FROM clause

Using Sequel I'd like to create an UPDATE statement like the following (from Postgres docs)
UPDATE employees SET sales_count = sales_count + 1 FROM accounts
WHERE accounts.name = 'Acme Corporation'
AND employees.id = accounts.sales_person;
However, looking at the Sequel documentation it's not clear where there's any way to create the FROM clause.
Is there a way to do this?
Thanks!
OK, Ben Lee's answer got me going in the right direction. Solution:
DB[:employees].from(:employees, :accounts).
where(:accounts__name => 'Acme Corporation').
update_sql(:employees__sales_count => "employees.sales_count + 1".lit)
I'm a bit uncertain about the use of .lit here, but it does seem to do the trick. (Also, I'm using update_sql rather than update to produce the SQL output rather than running the command.)
Result, as desired:
UPDATE "employees"
SET "employees"."sales_count" = employees.sales_count + 1
FROM "accounts"
WHERE ("accounts"."name" = 'Acme Corporation')
Everybody else's answers have one tiny superfluous clause.
DB.from(:employees, :accounts).
where(:accounts__name => 'Acme Corporation').
update_sql(:employees__sales_count => "employees.sales_count + 1".lit)
If you're doing a from() with two tables, then the one inside the DB[...] is ignored.
Can you just use a regular join? It's not exactly your query, but it should accomplish your goals, right?
DB[:employees].left_outer_join(:accounts, :sales_person => :id).
where('accounts.name' => 'Acme Corporation').
update('employees.sales_count' => 'employees.sales_count' + 1)

Doctrine Query <timestamp

I have a colum "datetime", like this: 2012-06-04 15:40:20.
I want to create a query in Doctrine that I get the data of previous time. Less than 2012-06-04 15:40:20. How can I realize that in Doctrine.
Sorry, I just have no clue.
If I understand your question correctly, I believe the syntax is just:
$datetime = // your timestamp
->where('t.somefield < ?', date('Y-m-d H:i:s', strtotime($datetime))
I am not familiar with Doctrine, but here is standard SQL to do what you want:
select *
from t
where t.datetime in (select max(datetime)
from t
where datetime < '2012-06-04 15:40:20'
)
If Doctrine supports standard SQL syntax, then something like this would work (you might have to input the date/time constant in a different way).