Forum: Ruby on Rails Fastest way of adding " " around multiline text in RADRAILS

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cd8c9864d88bcafc164d8fdb820cc451?d=identicon&s=25 Chris (Guest)
on 2006-03-28 17:24
lets say i have the following SQL in my database editor :
When i paste it into rad rails i have to add quotes and the '+' symbol
to the end of each line.  It is very tedious.  What is the best method?

select timesheets.employee,
sum(items.hours) as hours,
sum(items.hours*timesheets.cost)
as cost,
sum(items.hours*timesheets.charge*decode(activities.chargetype,0,1,0))
as charge,
sum(items.hours*decode(activities.chargetype,0,1,0)) as chargehours
from timesheets, contacts, items, activities, employees, divisions
where
contacts.com_branch_id = 241 and
employees.contact_id = contacts.CONTACT_ID and
contacts.expired=0 and
timesheets.id = items.timesheet and
activities.id = items.activity and employees.id=timesheets.employee and
divisions.id=employees.division and timesheets.status = 3 and
timesheets.start_date >= ? and timesheets.start_date < ? group by
timesheets.employee

Thanks,
 Chris
A9d77f57187ac17ae968d8bd2186d4ad?d=identicon&s=25 Don Walker (walkins)
on 2006-03-28 17:49
(Received via mailing list)
Give this a try:

sql_string = <<-_SQL
select timesheets.employee,
sum(items.hours) as hours,
sum(items.hours*timesheets.cost)
as cost,
sum(items.hours*timesheets.charge*decode(activities.chargetype,0,1,0))
as charge,
sum(items.hours*decode(activities.chargetype,0,1,0)) as chargehours
from timesheets, contacts, items, activities, employees, divisions
where
contacts.com_branch_id = 241 and
employees.contact_id = contacts.CONTACT_ID and
contacts.expired=0 and
timesheets.id = items.timesheet and
activities.id = items.activity and employees.id=timesheets.employee and
divisions.id=employees.division and timesheets.status = 3 and
timesheets.start_date >= ? and timesheets.start_date < ? group by
timesheets.employee
_SQL
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-29 14:18
(Received via mailing list)
Chris wrote:
> sum(items.hours*decode(activities.chargetype,0,1,0)) as chargehours
> from timesheets, contacts, items, activities, employees, divisions
> where
> contacts.com_branch_id = 241 and
> employees.contact_id = contacts.CONTACT_ID and
> contacts.expired=0 and
> timesheets.id = items.timesheet and
> activities.id = items.activity and employees.id=timesheets.employee and
> divisions.id=employees.division and timesheets.status = 3 and
> timesheets.start_date >= ? and timesheets.start_date < ? group by
> timesheets.employee

Try using this approach (known as a "here document"):

sql = <<'ENDSQL'
select timesheets.employee,
sum(items.hours) as hours,
sum(items.hours*timesheets.cost)
as cost,
sum(items.hours*timesheets.charge*decode(activities.chargetype,0,1,0))
as charge,
sum(items.hours*decode(activities.chargetype,0,1,0)) as chargehours
from timesheets, contacts, items, activities, employees, divisions
where
contacts.com_branch_id = 241 and
employees.contact_id = contacts.CONTACT_ID and
contacts.expired=0 and
timesheets.id = items.timesheet and
activities.id = items.activity and employees.id=timesheets.employee and
divisions.id=employees.division and timesheets.status = 3 and
timesheets.start_date >= ? and timesheets.start_date < ? group by
timesheets.employee
ENDSQL

The ENDSQL can be any string you like - it just has to be the same
at the start and end. Putting it in single quotes, <<'ENDSQL', prevents
the kinds of interpretation of escape sequences and expression
interpolation that Ruby would do on a double-quoted string.

regards

   Justin
This topic is locked and can not be replied to.