I am trying to run a pair of sql statements with the second using a
value form the first's returned data set but can figure out the syntax.
@risks returns the values I would expect but @changes gets no returned
data obvioulsy because it doesn't see the contenst of @risks.id.
def reportbyowner2
@risks = Risk.find_by_sql(["SELECT * FROM risks r where r.status =
'Open' and r.owner_id = ? order by r.id", @params['owner']])
@changes = Change.find_by_sql(["SELECT * FROM changes c where c.risk_id
= ? order by c.id", @risks.id])
end
How many records are there in the @risks object. You’re doing c.risk_id=@risks.id, so if there are multiple rows in @risks then the query wont return any result.
@changes = Change.find_by_sql(["SELECT * FROM changes c where c.risk_id
= ? order by c.id", @risks.id])
How many records are there in the @risks object. You're doing
c.risk_id=@
risks.id, so if there are multiple rows in @risks then the query wont
return
any result.
The @risks object will have multiple risks. What I am trying to do is
for each risk I select I print the risk details then go and get any
changes records associted with that risk record and print them then go
round again for the next risk.
Can you advise the syntax I would need to use to achieve this.
>> @changes = Change.find_by_sql(["SELECT * FROM changes c where c.risk_id
>> = ? order by c.id", @risks.id])
> How many records are there in the @risks object. You're doing
> c.risk_id=@
> risks.id, so if there are multiple rows in @risks then the query wont
> return
> any result.
>> end
>> Any help please.
>> Many thanks.
>> --
>> Posted viahttp://www.ruby-forum.com/.
Shiv,
The @risks object will have multiple risks. What I am trying to do is
for each risk I select I print the risk details then go and get any
changes records associted with that risk record and print them then go
round again for the next risk.
Can you advise the syntax I would need to use to achieve this.
I like using the ActiveRecord methods instead of raw sql, so here it
goed:
I assume that you have these 3 model:
class Owner < ActiveRecord::Base
has_many :risks
end
class Risk < ActiveRecord::Base
belongs_to :owner
has_many :changes
end
class Change < ActiveRecord::Base
belongs_to :risk
end
def reportbyowner2
@owner = Owner.find(params[:owner])
@risks = @owner.risks.all(:conditions => { :status => 'Open'})
@risks.each do |risk|
risk.print_the_details
risk.changes.each{|change| change.print_the_details }
end
end