Ruby / Rails .first .last .current?

时间:2012-05-05 13:07:31

标签: ruby-on-rails ruby ruby-on-rails-3

Ruby / Rails中有.current这样的东西吗?

我在Release模型中有以下内容接受轨道作为嵌套属性。我正在使用:after_add手动设置has_many通过连接表中的位置列。理想情况下,我希望从我的表单的fields_for部分发送的position属性或者在保存时从track表/模型中的值集复制它。

我可以让它设置所有条目的第一个或最后一个位置,但不能设置与该轨道相关的当前位置吗?

我理想情况下需要 releases_tracks.each {| t | t.position = self.tracks.last.position} 就像 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

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

不能肯定地说我是否理解正确,但据我所知,releases_tracks.each { |t| t.position = t.track.position }应解决您的问题。

belongs_to - has_many关系有两种方式,因此对于这种关系约束的两个模型«所有者»和«归属»,Owner.first.belongingBelonging.last.owner查询都是有效的。