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(开发者_JS百科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
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.
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
精彩评论