On Sunday 08 March 2009, Michael Schuerig wrote:
> Anders gesagt, ich möchte AR dazu bringen, SQL zu erzeugen, das etwa
> so aussieht:
>
> SELECT ... FROM people
> JOIN roles ON roles.person_id = people.id
> JOIN movies ON roles.movie_id = movies.id
> JOIN role_types ON roles.role_type_id = role_types.id
> WHERE role_types.name = 'Director'
So kommt zumindest im Ergebnis heraus, was ich haben möchte:
class Person < ActiveRecord::Base
has_many :roles, :include => :role_type
module RoleTypeExtensions
def as_actor
self.find(:all,
:joins => 'JOIN role_types ON roles.role_type_id =
role_types.id',
:conditions => { :role_types => { :name => name }})
end
end
has_many :movies, :through => :roles, :extend => RoleTypeExtensions
named_scope actors,
:order => 'lastname, firstname',
:joins => { :roles => :role_type },
:conditions => { :roles => { :role_types => { :name => 'Actor' }}}
named_scope :with_movie_in_year, lambda { |year|
{
:joins => { :roles => :movie },
:conditions => ["movies.release_date between date(':year-01-01')
and date(':year-12-31')",
{ :year => year }]
}
}
...
end
@actor.movies.as_actor
Person.actors.with_movie_in_year(2004)
Michael
--
Michael Schuerig
mailto:[email protected]
http://www.schuerig.de/michael/
_______________________________________________
rubyonrails-ug mailing list
[email protected]
http://mailman.headflash.com/listinfo/rubyonrails-ug