Ich bei einer habtm-Assoziation Schwierigkeiten dadurch, dass offenbar
im assoziierten Objekt die Assoziation auf das Besitzer-Objekt nicht
gesetzt wird.
class Movie < AR::Base
validates_presence_of :year
has_and_belongs_to_many :awards
end
class Award < AR::Base
validates_presence_of :year
has_and_belongs_to_many :movies
def before_validation
if year.blank? && movies.size == 1
self.year = movie[0].year
end
end
end
Dann *sollte* es möglich sein, einen Award zu einem Movie auf diese
Weise zu erzeugen
award = movie.awards.create!(...)
Das geht aber leider nicht, denn award.movies wird nicht gesetzt
(movie.awards auch nicht). Der unschöne Workaround ist
award = Award.create!(:movies => [movie], ...)
Fällt noch jemand ein eleganterer Weg ein?
Michael
--
Michael Schuerig
mailto:michael@schuerig.de
http://www.schuerig.de/michael/
on 2009-03-24 16:53
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.