Hi All,
As a follow-up to what was discussed below I''ve created a patch that
allows to use belongs_to attributes in dynamic finders, but what''s
more:
it introduces dynamic scopes.
For more details see:
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/668-dynamic-scopes-and-dynamic-finders-for-belongs_to
Lawrence
Michael Koziarski wrote:>> I think it would be nice to be able to do:
>>
>>
Comment.university(boston).creator(current_user).commentable(post).all
>>
>
> Indeed, that does look pretty nice, and very useful in some
> circumstances. But it could be implemented without touching the query
> generation code at all. You could automatically add proc based named
> scopes to the class when you define the associations. and allow those
> procs to take either ids or instances.
>
> I''d definitely prefer that approach to supporting hash keys
referring
> to associations inside the query generation code.
>
>
>>
Comment.find_all_by_university_id_and_creator_id_and_commentable_type_and_commentable_id(boston,
>> current_user, post.class.base_class.name.to_s, post.id)
>>
>
> I''d never advocate anything quite as ugly as that. but something
like
>
> post.comments_by_user_about(current_user, boston)
>
> would work fine too.
>
>
>> It''s main advantage would be that it allows easy formation of
dynamic
>> queries based on input:
>>
>> scope = Comment.scoped({:include => [:creator, :commentable]})
>> scope = scope.commentable(post) if post
>> scope = scope.university(params[:uni]) if params[:uni]
>> scope = scope.creator(current_user) if params[:created_by_me] ==
"1"
>> scope = scope.country(params[:country]) if params[:country]
>> @comments = scope.all
>>
>
> This is a nice use case, care to work with nik on implementing it?
>
>
--~--~---------~--~----~------------~-------~--~----~
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 rubyonrails-core@googlegroups.com
To unsubscribe from this group, send email to
rubyonrails-core-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en
-~----------~----~----~----~------~----~------~--~---