A Song :has and belongs to many Playlists. I want to store additional information about a song in a given playlist, i.e. the number of request of this song in a playlist. Here is an example Playlist a: |Song A|5 requests| |Song B|3 requests| Playlist b: |Song A|2 requests| |Song B|4 requests| In SQL I would add an extra column :number_of_requests to the songs_playlists table. But how do I do it with pure ActiveRecord?
Gleb M. wrote:> A Song :has and belongs to many Playlists. > I want to store additional information about a song in a given > playlistThen you''ll need has_many :through. HABTM doesn''t deal with extra fields in the join table. Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
2009/7/25 Glex <glex.spb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > A Song :has and belongs to many Playlists. > I want to store additional information about a song in a given > playlist, i.e. the number of request of this song in a playlist. > > Here is an example > > Playlist a: > |Song A|5 requests| > |Song B|3 requests| > > Playlist b: > |Song A|2 requests| > |Song B|4 requests| > > In SQL I would add an extra column :number_of_requests to the > songs_playlists table. > But how do I do it with pure ActiveRecord?You can make the join table be a fully fledged ActiveRecord model (it will need an id column) and add the new columns to it. Then use has_many :through rather then habtm Colin
On Jul 25, 9:21 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas- s.net> wrote:> Gleb M. wrote: > > A Song :has and belongs to many Playlists. > > I want to store additional information about a song in a given > > playlist > > Then you''ll need has_many :through. HABTM doesn''t deal with extra > fields in the join table. > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://www.ruby-forum.com/.How would I do this? I am new to Rails. :has_one request_count :through => "what do i put here? where do I store it?" Thank you
2009/7/25 Glex <glex.spb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Jul 25, 9:21 pm, Marnen Laibow-Koser <rails-mailing-l...@andreas- > s.net> wrote: >> Gleb M. wrote: >> > A Song :has and belongs to many Playlists. >> > I want to store additional information about a song in a given >> > playlist >> >> Then you''ll need has_many :through. HABTM doesn''t deal with extra >> fields in the join table. >> >> Best, >> -- >> Marnen Laibow-Koserhttp://www.marnen.org >> mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> -- >> Posted viahttp://www.ruby-forum.com/. > > How would I do this? I am new to Rails. > :has_one request_count :through => "what do i put here? where do I > store it?" >Have a look at the rails guides at http://guides.rubyonrails.org/ particularly the one on ActiveRecord Associations (assuming you have already looked at the Getting Started one). Google will provide lots of help too. Colin