Em 04-05-2012 07:48, Jon Leighton escreveu:
There has been discussion about putting a new syntax into Rails 4 that
more closely resembles scopes. Example:
has_many :comments, -> { where(deleted: false).order(:created_at) }
For this particular case, I don't understand. What would be the
advantage of lazy evaluation?
Take a look at how some examples are handled in Sequel with regards to
associations:
http://sequel.rubyforge.org/rdoc/files/doc/association_basics_rdoc.html
See section Association Options
So, for that particular case, a single block would be enough without the
need for a proc. Here is an example using Sequel:
many_to_one(:comments){|ds| ds.where(deleted: false).order(:created_at) }
Procs would be useful for some cases where you depend on some dynamic
data (taken from the Sequel documentation with a small typo fix):
Artist.one_to_many :songs, dataset: -> {
Song.select(:songs.*).join(Lyric, id: :lyric_id,
id => [:composer_id, :arranger_id, :vocalist_id, :lyricist_id])
}
Artist.first.songs_dataset
# SELECT songs.* FROM songs
# INNER JOIN lyrics ON
# lyrics.id = songs.lyric_id AND
# 1 IN (composer_id, arranger_id, vocalist_id, lyricist_id)
Also, I much prefer such approach as a generalized one than the AR
'finder_sql' option in a sense that it could be more easily cascaded
with other conditions.
Am I missing something?
Cheers,
Rodrigo.
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.