rails保存父类数据

时间:2013-03-03 19:36:37

标签: ruby-on-rails ruby save associations

我正在通过子对象更新信息,但我似乎无法获取其父对象来保存数据..这就是情况。

class NewsPage < Content
   content_fields :body

内容有一个列调用体,所以我可以这样做:

newsPage1.body

我想将page2的主体复制到page1:

newsPage1.body << newsPage2.body

如果我打印newsPage1.body我会在那里得到两个主体..但是一旦请求完成,newsPage1.body将重置为其原始状态..我认为是因为我没有访问Content的表时发生...所以如何将newsPage1的内容保存到Content的页面列?我可能错了,这可能是由于其他东西..和这个人一样的问题,但他从来没有得到答案: @article.save seems save the old content not the updated one

已编辑以添加我之前链接的帖子中的代码:

  def my_method(other_id)
    self.rating << NewsPage.find(other_id).rating
    self.body << NewsPage.find(other_id).body
    return self   
  end

在我的控制器中,这被定义如下:

  def copy 
    @newsPage = NewsPage.find(params[:id])
    @copiedNewsPage = @newsPage.my_method(params[other_id])

    redirect_to :action => 'main'   
  end

所以评级复制好了,但身体没有,任何线索?

1 个答案:

答案 0 :(得分:0)

我正在努力理解你的问题,并且用你编写的几行代码我也很难知道你在做什么,尝试添加更多代码并解释一下更好的问题。如果其他人无法重现您的问题,那么他将无法帮助您。

但这听起来像是一个群发属性问题,试试这段代码:

class NewsPage < Content
  attr_accessible :body
  content_fields :body

让我知道它是否有效。 THX