My solution works, but I wonder if there is a better one.

I have a model (:cards), which has a foreign key :box_id. I am
interested in the id's of those cards which have a certain box_id.
Currently I assume that I can easily hold an array of all thos :cards in
memory (i.e. no cursor needed).

This is my current solution:

Card.where(box_id: params[:box_id]).map {|c| c.id }.each do |cid|
  # .... Do something with cid
end

This is compact, but I don't like the fact that first, all data from the
retrieved Card objects needs to be stored in memory at least temporarily
(there is even a 'text' field in Card!), but only the id is needed.

I thought as an alternative to use find_by_sql, but the API
documentation warns that this should be only used as a "last resort",
because it makes us dependent on the syntax for a particular database.
Although in my case, the SQL query would be so simple that I don't fear
I would run into compatibility problems when exchanging one database for
another, I wonder whether there is a simpler solution, using just
ActiveRecord functions.

Ronald

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/05a79fc3b1b9e3ea9a0c7b650c5b5e81%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to