English 中文(简体)
• 通过协会
原标题:Saving the order of associated records in a Rails has_many :through association

I m在铁路码头上工作,包括一种方式,通过协会修改相关记录的顺序。 我们有以下模式:

class Playlist < ActiveRecord::Base
  has_many :playlists_songs, :dependent => :destroy
  has_many :songs, :through => :playlists_songs
end

class Song < ActiveRecord::Base
  has_many :playlists_songs, :dependent => :destroy
  has_many :playlists, :through => :playlists_songs
end

class PlaylistsSong < ActiveRecord::Base
  belongs_to :playlist
  belongs_to :song
end

如果我们改变游览的顺序(例如@playlist.songs.rotate!), 铁路没有触及游艇上的记录,因此具有意义。 我愿向歌唱歌唱团发出任何呼吁,以拯救歌唱的顺序,但也许可以删除歌剧中现有的相关行号,并按适当的顺序创建新行(<代码>:order =>“id”可在检索时加以使用),或者增加一个:打字栏,以 play歌唱歌,并相应地更新这些价值观。

I didn t see any callbacks (e.g. before_add) that would allow this. In ActiveRecord::Associations::CollectionAssociation, the relevant methods seem to be writer, replace, and replace_records, but I m lost on what the best next step would be. Is there a way to extend or safely override one of these methods to allow for the functionality I m seeking (preferably for only specific associations), or is there a different, better approach for this?

最佳回答

您是否在

它不是在<代码>id上分类,而是在立场栏上分类。 然后,它只是更新立场的问题,而不是更改<条码>id或删除/替换记录这一重大事项。

在您的情形下,你只是在<代码>PlayListSong栏上添加一个<代码>,然后:

class PlayListSong
  acts_as_list :scope => :play_list_id
end

如你在评论中指出的,<编码>方法_as_list 大部分关于清单中单个项目的工作,没有箱子上的“顺序”功能。 我不建议修改<代码>replace_records,以便做到这一点。 更清洁和更明确的做法是,采用与原始物相同的职位栏。 例如。

class PlayList
  # It makes sense for these methods to be on the association.  You might make it
  # work for #songs instead (as in your question), but the join table is what s
  # keeping the position.
  has_many :play_list_songs, ... do

    # I m not sure what rotate! should do, so...

    # This first method makes use of acts_as_list s functionality
    #
    # This should take the last song and move it to the first, incrementing 
    # the position of all other songs, effectively rotating the list forward 
    # by 1 song.
    def rotate!
      last.move_to_top unless empty?
    end

    # this, on the other hand, would reorder given an array of play_list_songs.
    # 
    # Note: this is a rough (untested) idea and could/should be reworked for 
    # efficiency and safety. 
    def reorder!(reordered_songs)
      position = 0
      reordered_songs.each do |song|
        position += 1

        # Note: update_column is 3.1+, but I m assuming you re using it, since
        # that was the source you linked to in your question
        find(song.id).update_column(:position, position)
      end
    end
  end
end
问题回答

暂无回答




相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签