When I look at this method and when I think to pursuit of beauty thanks
Ruby, I think I could do a nicer bunch of code…
I would like to seperate racks already added in the showed room and the
others
Here is the only method I could find but as a beginner I surely miss
some tricks!
Please, can someone give some hints?
def show
@room = Room.find(params[:id])
@racks = Rack.find( :all )
@racks_room = Array.new
@free_racks = Array.new
for rack in @racks do
if rack.room_id == @room.id
@room_s_racks << rack
else
@free_racks << rack
end
end
end
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of arnaud stageman
Sent: Tuesday, June 13, 2006 10:33 AM
To: ruby-talk ML
Subject: ugly ruby code…
I would like to seperate racks already added in the showed
room and the others
[…]
You seem to use only instance variables. If you are not going to need
them later in views, I would prefer using local variables because they
go out of scope when the method finishes and don’t clutter the object
with unnecessary variables. So, just replace @racks with racks.
Replace the long Array.new with the shorter form []. Of course this is
just my opinion, but I find it easier to type and easier to read as it
gives you some kind of visual clue that there’s an array involved.
-----Original Message-----
From: [email protected]
[mailto:[email protected]] On Behalf Of arnaud stageman
Sent: Tuesday, June 13, 2006 10:33 AM
To: ruby-talk ML
Subject: ugly ruby code…
I would like to seperate racks already added in the showed
room and the others
[…]
You seem to use only instance variables. If you are not going to need
them later in views, I would prefer using local variables because they
go out of scope when the method finishes and don’t clutter the object
with unnecessary variables. So, just replace @racks with racks.
Replace the long Array.new with the shorter form []. Of course this is
just my opinion, but I find it easier to type and easier to read as it
gives you some kind of visual clue that there’s an array involved.
Yes you’re right I use now racks instead of @racks, thanks.
“The array form is to be used when the condition input is
tainted and requires sanitization”
I wouldn’t think of an id derived from another table as been tainted.
Perhaps I’m wrong, but please explain if this is the case. I don’t think
you can store malicious code in an NUMERIC column?
The first solution is better because you execute only one sql request.
I don’t buy that without seen benchmark results. Iterating in pure ruby
while creating two new arrays (increasing the size each iteration) is
hardly faster than executing an SQL statement. (those db guys are realy
speed freaks :))
“The array form is to be used when the condition input is
tainted and requires sanitization”
I wouldn’t think of an id derived from another table as been tainted.
Perhaps I’m wrong, but please explain if this is the case. I don’t think
you can store malicious code in an NUMERIC column?
This code will one day be copied over to some other place where this
id is tainted. Then you’l have nightmare on yur hands
“The array form is to be used when the condition input is
tainted and requires sanitization”
I wouldn’t think of an id derived from another table as been tainted.
Perhaps I’m wrong, but please explain if this is the case. I don’t think
you can store malicious code in an NUMERIC column?
Honestly I don’t have any clue about the good and bad method but it
costs nothing to do I believe
The first solution is better because you execute only one sql request.
I don’t buy that without seen benchmark results. Iterating in pure ruby
while creating two new arrays (increasing the size each iteration) is
hardly faster than executing an SQL statement. (those db guys are realy
speed freaks :))
Yes you’re right. I had not see the problem like that… It would need
further investigations.
As my app will be used in really small production environment, I think
I’ll get your solution 'cause I find it more clear!
I wouldn’t think of an id derived from another table as been tainted.
Perhaps I’m wrong, but please explain if this is the case. I don’t think
you can store malicious code in an NUMERIC column?
Well, in this case you are probably correct in assuming that that
particular data is safe, but it’s not necessarily the case. That
would be okay given that you check all your data before you store it
in the database, but I wouldn’t rely on the database for type safety.
What if you’re using SQLite w/o strict affinity mode (which is the
default)? If you don’t do any validation in your application and you
just try to stick in that column, SQLite will be happy to do it for
you. You might not be so happy of course =)
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.