Rails 3 saving a record issue - ruby-on-rails-3

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

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

Related

Get current user in a model in Rails3

I am using ActiveAdmin. I need to store the User that created or updated every record.
So I added user_created_id and user_updated_id to every model.
On before_save I want to store the information about current_admin_user. The problem is that I cannot access current_admin_user in a model.
Is there a way to do that without breaking MVC?
I found this: http://rails-bestpractices.com/posts/47-fetch-current-user-in-models
But I am not sure if it is safe.
Any help?
Do it in controller (it's the reason that controller exist) before save:
def create
#my_model = MyModel.create params[:my_model]
#my_model.user_created = current_user
#my_model.save
end
The same with the update method.

Rails script not updating all mongoid documents

Currently, I have a User object that has a field realname. I'm trying to split on the space and convert it to two fields, first_name and last_name. Below is the script that I wrote to do this:
User.all.each do |user|
puts "Updating #{user.realname}"
name = user.realname.split(' ')
user.first_name = name[0]
user.last_name = name[1]
user.save
puts "Saved #{user.first_name} #{user.last_name}"
sleep(1)
end
When I run this using rails runner in my development environment, many of the first half of the users aren't not getting updated. Although the output from the script is perfect, when I look at what is saved in mongo, some users don't have these new fields.
Are you sure those users are valid?
Try using user.save! instead of user.save.
If you want to save this nevertheless then you can bypass validations by calling:
user.save(validate: false)
Try looking for a changed default_scope on your User class.
If the default scope have been changed, then User#All will not mean "all objects available on DB".

Paperclip not saving files with save()

I have a little problem with paperclip saving the data passed through the form...
If I'm trying to save the record with .save() it won't save.. When I look in the server/log there are no errors or warnings for the paperclip gem :-/
# trying to save the record with save() -- not working :-/
def create
#baan_import = BaanImport.new(params[:baan_import])
if #baan_import.save
redirect_to(baan_imports_url)
else
render 'new'
end
end
Server-log: (using .save() in controller)
https://gist.github.com/1327347
I just don't get it why it's working if I'm using .create instead of .save()
# trying to save the record with Model.create() -- working!
def create
#baan_import = BaanImport.create(params[:baan_import])
redirect_to(baan_imports_url)
end
Server-log: (using .create() in controller)
https://gist.github.com/1327359
Can some one explain me why it's working with create and not with save??
Thanks,
Michael
Can you show us the BaanImport model. My first guess is you're possibly missing baan_upload in attr_accessible on your model, and as a result, Rails will not let you mass assign the file parameter for upload.
Can you also confirm (would appear as though it's properly set up) that your form has html => {:multipart => true} as an option?

Ruby on rails tutorial: where is the user create view created?

Trying to follow along the ruby on rails 3 tutorial, I've completed the sign up process (through chapter 8) but nowhere has a user view: create.html.erb been created. Since 'create' is a section in the user controller, there needs to be a corresponding 'create.html.erb' file in views/users so I get a 'missing template' error when I try to use the tutorial form to create a new user.
Where does the tutorial create the create.html.erb file? I have paged through the tutorial a couple of times and can find no trace of it.
Thanks in advance for any help. --Fred
I am editing the question because for some reason the site will not let me add a comment below the responses to my question.
In UsersController... I have copied the following code directly out of the tutotrial:
def create
#user = User.new(params[:user])
if #user.save
#do something here
else
#title = "Sign up"
render = 'new'
end
end
I get: missing template users/create with {:locale=>[:en, :en].....
So if It is not a missing template issue, what is it?
create is an intermediary action, it does not require a view. in your create function make sure you do a redirect_to or a render view to something. here you can also populate a flash with messages corresponding to the success of the function (validation errors, succes for save etc)
When you create a new record, you actually call the new method, which does have an associated view called new.html.erb. The create method is called when you hit the "submit" button in the new.html.erb view, and there is no view for create - it is just a method that runs without a view.
Same applies for the update method which doesn't have it's own view, but which is "friends" with the edit action, whose associated view is called edit.html.erb.
Delete method is different

Rails 3: Manipulating devise "resource" in controller?

I'm using devise & devise_invitable in a rails 3 project, and I'm trying to manipulate some of the 'User' object fields in the devise controller.
The action is question is this:
def update
self.resource = resource_class.accept_invitation!(params[resource_name])
resource.first_name = 'Lemmechangethis'
if resource.errors.empty?
set_flash_message :notice, :updated
sign_in(resource_name, resource)
respond_with resource, :location => after_accept_path_for(resource)
else
respond_with_navigational(resource){ render_with_scope :edit }
end
end
I'd have thought that the (commented out) resource.first_name call would influence resource in much the same way as a model - but it doesn't seem to. I'm still getting a 'blank' validation error on this form.
So, the question is, how do I specify values to the User model in devise (and/or devise_invitable) that will actually be subject to verification?
Any suggestions appreciated,
John
resource does return a User models instance. So the resource.first_name = 'Lemmechangethis' statement does change your User models instance but it doesnot trigger your User models validations, which is probably why resource.errors always returns an empty array. One way to trigger the User models validation is to call resource.valid? for example. You can then check the resource.errors array for specific error messages.