I have added new fields for my devise user, but, now, I can't update the student record, although I get the message:
You updated your account successfully.
But, the DB is never updated!
The new fields have attr_accessible and attr_accessor
Is it because there is a foreign keys in the new fields ? I have added country id to associate the user with his country, is this a reason for not updating ?
How can I debug the DB error occurred ? I tried to use update_attributes! in the devise function: update_with_password, but, no luck, no errors, just: You updated your account successfully.
I've noticed that there is no SQLite UPDATE command issued at Server Development log, why ?
Any help please ?
I found the solution, I should not use attr_accessor as its for those attributes that are not stored directly into DB.
I hope this will help some one.
Try raising a exception or logging by adding an after_filter to your update action on the UserController. Try overriding the controller action (and call super) if you dont have a hook to that code.
Related
I have set up a very simple rails 5 project to narrow down my problem:
https://github.com/benedikt-voigt/capybara_js_demo
In this project the data mutation done by the Capybara JS is not deleted, neither by Rails nor by the Database cleaner I added.
The following great blog argues, that no DatabaseCleaner is needed:
http://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails
but this works only for fixtures, not for the mutation done by an out-of-thread Capybara test.
I added the Database cleaner, but this also needed work.
Does anybody has a sample setup?
From a quick look at your test I would say it's leaving data because the data is actually being added after DatabaseCleaner cleans. The click_on call occurs asynchronously, so when your assert_no_content call happens there's no guarantee the app has handled the request yet or the page has changed yet and since the current page doesn't have the text 'Name has already been taken' on it the assertion passes and the database gets cleaned. While that is happening the click gets processed by the app and the new data is created after the cleaning has occurred. You need to check/wait for content that will appear on the page after the click - something like
page.assert_text('New Foo Created')
You should only be asserting there is no content once you already know the page has changed, or you're expecting something to disappear from the current page.
I solved now the problem by setting the DB connection to one
class ActiveRecord::Base
mattr_accessor :shared_connection
##shared_connection = nil
def self.connection
##shared_connection || ConnectionPool::Wrapper.new(:size => 1) { retrieve_connection }
end
end
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection
as describe here:
https://mattbrictson.com/minitest-and-rails
I uploaded the working repo here:
https://github.com/benedikt-voigt/capybara_js_demo_working
I'm developing a Rails application that has a background process that updates some user information. In order to do so, this method has to delete all the existing information (stored in another table) and get new information from the Internet. The problem is that if something goes wrong in the midtime users don't have information until the process runs again.
there is something to do like:
transaction = EntityUser.delete_all(:user_id => #current_user.id)
#instructions that adds new entity
...
...
transaction.commit
can anyone suggest something that i can do to avoid this kind of problem?
thank you
Read about ActiveRecord::Transactions. You can wrap everything in a block
ActiveRecord::Base.transaction do
EntityUser.delete_all(:user_id => #current_user.id)
# ...
end
If something goes wrong, you can call raise ActiveRecord::Rollback within that block to cause all changes to the database within the transaction block to be reverted.
Another thought is to soft-delete the EntityUser instance(s) for a User instead of actually removing them from the database. There are gems to help you with this functionality such as acts_as_paranoid.
You would
soft-delete the EntityUser instance(s)
Fetch and build the new EntityUser instance(s)
(Optionally) flush the soft-deleted instance(s) from the database if everything went well
If something goes wrong using this method it's easy to just un-delete the soft-deleted records.
There's got to be an easy way to do this, but I cannot find an answer...
When some creates or updates a WorkRequest in my app, I do other processing, including creating a Workflow object. I do some checking to make sure, for example, there isn't more than one Workflow already created for this WorkRequest. If there is, I want the update/create to fail with an error message. I just can't see how to do this. I tried returing false from my before_update callback method, but this did not work.
Do I raise an error and rescue it in my controller? What is the right way to do this in Rails 3?
Any help would be much appreciated.
This is why you have validations. You can implement an own validation like this:
class ...
validate :my_validation
def my_validation
if workflows > 1
errors.add(:workflow, "cannot be more than one" )
end
end
end
I am getting this error:
ActionDispatch::Cookies::CookieOverflow
In config/application.rb I have:
config.session_store :active_record_store
There is no data being stored in the sessions table.
I am using RubyCAS-client with this code:
https://github.com/zuk/rubycas-client-rails
Am I doing something wrong to have the code stored in the database or is this code not setup to use active record store?
I figured it out, I was doing it in config/application.rb. When I changed it in config/initializers/session_store.rb it worked.
I don't know if this is completely right because I assume config/application.rb is the place it should be?
I am using Rails 3 and AJAX and have a parent object which is being created through and AJAX request. This parent is sent with children and saves them all. However, if a child has an error, Rails will stop the request. Is there any way to tell Rails to ignore this? I understand the proper thing to do is find the problem within the Javascript sending the request and fix it. However, for the sake of learning, is there a way to tell Rails that some errors might be ignorable?
To save without validating use:
#parent.save(:validate => false)
Also, don't forget you can create conditional validation rules if needs be. For example, add a virtual attribute (an instance variable that is not persisted to the DB) accessible via bare_bones?. Then modify a validator like so:
validates_presence_of :nickname, :unless => "bare_bones?"
Then, in your controller you would do something like this:
#parent = Parent.new params[:parent]
#parent.bare_bones = true
#parent.save()
Hope this helps.
You are looking for exception handling.
begin
#code that may contain errors
rescue
#what to do if an error is encountered
end