Rails 3 保存记录问题
我一直在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不包含新添加的记录,因为您在分配
@some_record.users.push(current_user)
后没有保存操作。您所要做的就是在分配后添加
.save
,它应该可以工作。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.您的问题尚不清楚模型结构,但我们假设
current_user
belongs_to
some_records
要强制读取数据库,请尝试以下操作:
默认情况下
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:
By default
forcereload = false
, to override this you should passtrue