Reputation: 1771
Is there such a thing as .current in Ruby/Rails?
I have the following in my Release model to accept tracks as nested attributes. I'm using :after_add to manually set the position column in the has_many through join table. I ideally want this to be populated from either the position attribute sent from the fields_for part of my form or copied from the value set in the tracks table/model on save.
I can get it to set the first or last positions on all entries, but not the current position that relates to that track?
I ideally need releases_tracks.each { |t| t.position = self.tracks.last.position } to be something like releases_tracks.each { |t| t.position = self.tracks.current.position }
has_many :releases_tracks, :dependent => :destroy, :after_add => :position_track
has_many :tracks, :through => :releases_tracks, :order => "position"
accepts_nested_attributes_for :tracks, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => :true
accepts_nested_attributes_for :releases_tracks
def position_track(track)
releases_tracks.each { |t| t.position = self.tracks.last.position }
end
Can anyone help?
Upvotes: 0
Views: 667
Reputation: 417
Can't say for sure if I understood you correctly, but, as far as I can tell, releases_tracks.each { |t| t.position = t.track.position }
should solve your problem.
belongs_to
- has_many
relationship works two ways, so for two models «Owner» and «Belonging» bound by such relationship both Owner.first.belonging
and Belonging.last.owner
queries are valid.
Upvotes: 1