Rails 3 保存记录问题

发布于 2024-10-26 16:34:21 字数 1048 浏览 0 评论 0原文

我一直在使用 Rails 3.0.5 和 Ruby 1.9.2,我注意到新记录不会保存或无法立即使用。

例如

def create    
    @some_record = Pool.new(params[:pool])  
      @some_record.users.push(current_user)
    if params[:commit] == "Add More"          
      @some_record.save        
      @some_record.do_something
    elsif params[:commit] == "Save"
      do_something_else(params)
    elsif params[:commit] == 'Cancel'
      redirect_to user_url(current_user)
    end    
 redirect_to some_other_url(current_user)
end

,当我保存记录并调用 some_record.do_something 时,保存的对象不会立即可用。 current_user.some_records 不包含新添加的记录,但 current_user.some_records.all 显示新保存的记录。但是,在控制台上,我可以使用 current_user.some_records 查看新创建的记录。

我确信我错过了 Rails 3 的一些基本内容。我也尝试了与 current_user.some_records.build(params[:some_record] 相同的方法,并且我遇到了同样的问题。Rails 3 是否保存了对象立即或存在某种延迟写入/保存,因为 Rails 3.0.3 不会出现同样的问题。

此外,我没有使用像 authlogic/devise 这样的身份验证插件,我只是将 current_user 对象保存到会话中。不确定我做错了什么。是否有任何帮助?

它也是 some_record 和用户之间的多对多关联。

I have been working with Rails 3.0.5 and Ruby 1.9.2 and I have noticed that a new record doesn't get saved or isn't available for use instantly.

For example

def create    
    @some_record = Pool.new(params[:pool])  
      @some_record.users.push(current_user)
    if params[:commit] == "Add More"          
      @some_record.save        
      @some_record.do_something
    elsif params[:commit] == "Save"
      do_something_else(params)
    elsif params[:commit] == 'Cancel'
      redirect_to user_url(current_user)
    end    
 redirect_to some_other_url(current_user)
end

So when I save the record and call some_record.do_something the saved object isn't available instantly. current_user.some_records doesn't contain the newly added record but current_user.some_records.all displays the newly saved record. However on the console I am able to view the newly created record with current_user.some_records.

I'm sure I am missing something fundamental to Rails 3. I have also tried the same with current_user.some_records.build(params[:some_record] and I have the same problem. Does Rails 3 save the object instantly or is there some kind of delayed write/save because the same problem does not occur with Rails 3.0.3.

Also I am not using an authentication plugin like authlogic/devise and I simply save the current_user object to the session. I am not sure what I am doing wrong. WOuld appreciate any help?

Its also a many-to-many association between some_record and users

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情痴 2024-11-02 16:34:21
current_user.some_records 

不包含新添加的记录,因为您在分配@some_record.users.push(current_user)后没有保存操作。
您所要做的就是在分配后添加 .save ,它应该可以工作。

current_user.some_records 

does not contain the newly added record because you did not save the operation after assigning @some_record.users.push(current_user).
All you have to do is to add .save after the assignment and it should work.

鸵鸟症 2024-11-02 16:34:21

您的问题尚不清楚模型结构,但我们假设 current_user belongs_to some_records
要强制读取数据库,请尝试以下操作:

current_user.some_records(true)

默认情况下 forcereload = false,要覆盖此设置,您应该传递 true

The model structure is not clear from your question but let's assumed that current_user belongs_to some_records
To force a database read try this:

current_user.some_records(true)

By default forcereload = false, to override this you should pass true

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文