我正在开发一个Rails插件,其中包含一种修改has_many中关联记录顺序的方法:通过关联.假设我们有以下型号:
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
Run Code Online (Sandbox Code Playgroud)
如果我们改变播放列表歌曲的顺序(例如@playlist.songs.rotate!),Rails不会触及playlists_songs表中的记录(我正在使用Rails 3.1),这是有道理的.我想调用播放列表的歌曲=方法保存歌曲的顺序,但是,可以通过删除播放列表中的相关现有行并以正确的顺序创建新的行(这样:order => "id"可以在检索它们时使用)或者通过向playlists_songs添加sort:integer列并相应地更新这些值.
我没有看到任何允许这样做的回调(例如before_add).在ActiveRecord :: Associations :: CollectionAssociation中,相关的方法似乎是writer,replace和replace_records,但我对最好的下一步将会失败.有没有办法扩展或安全地覆盖这些方法之一,以允许我正在寻找的功能(最好只针对特定的关联),或者是否有一个不同的,更好的方法呢?
activerecord ruby-on-rails associations has-many-through ruby-on-rails-plugins
有没有办法在routes.rb或控制器中获取URL的哈希值(例如/ posts/show#35中的"35")?
我的印象是这从未发送到服务器,但我只是想确定.
谢谢!
汤姆
我使用本教程创建了一个包含来自某些JSON的记录的SQLite数据库,我想使用MagicalRecord来查询它.
MagicalRecord看到NSManagedObject(BlogPost)并且可以创建记录,但是它没有看到预先填充的记录,所以我猜它没有看到我添加的SQLite文件.我已经验证了SQLite数据库确实包含使用SQLite客户端的行.
在application:didFinishLaunchingWithOptions:,我把:
[MagicalRecord setupCoreDataStackWithStoreNamed:@"DBG.sqlite"];
Run Code Online (Sandbox Code Playgroud)
并在applicationWillTerminate::
[MagicalRecord cleanUp];
Run Code Online (Sandbox Code Playgroud)
当我[BlogPost MR_findAll]在控制器中调用时,它返回一个空集.DBG.sqlite位于项目目录的根目录下,我尝试将其放入"Copy Bundle Resources"中,但blogPosts仍然返回一个空集.
有任何想法吗?