Created New User - User Exists? - ruby-on-rails

I went to create a user=User.new (:screen_name => "jeff holmes", :email "jeff92#jeff.com", :password "1234") I had some validations I was trying to see if it would pass:
class User < ActiveRecord::Base
validates_uniqueness_of :screen_name, :email
validates_length_of :screen_name, :within => 4...20
validates_length_of :password, :within => 4...20
validates_length_of :email, :within => 6...50
def validate
errors.add(:email, 'must be valid.') unless email.include? ("#")
errors.add(:screen_name, 'cannot include spaces.') if screen_name.include?(" ")
end
end
After I try to create the user all this stuff pops up and I have no idea what it is.
NameError: undefined local variable or method `user' for main:Object
from (irb):3
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/console.rb:110:in `start'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/console.rb:9:in `start'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/railties-4.2.1/lib/rails/commands.rb:17:in `<top (required)>'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `block in require'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:274:in `require'
from /Users/coreyholmes/RubymineProjects/worklink/bin/rails:8:in `<top (required)>'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.1/lib/active_support/dependencies.rb:268:in `load'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/commands/rails.rb:6:in `call'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/command_wrapper.rb:38:in `call'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:183:in `block in serve'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `fork'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:156:in `serve'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:131:in `block in run'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `loop'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application.rb:125:in `run'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/spring-1.3.6/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /Users/coreyholmes/.rbenv/versions/2.2.2/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
Then after all that stuff pops up I go user.save and this appears.
NameError: undefined local variable or method `user' for main:Object
Whats going on? What am I doing wrong here.
Thanks

Not 100% certain on the problem at hand but here's a little overview of the create process
user = User.new # creates the user object/instance but DOES NOT save or populate the ID column/attribute/method
# OR ...
user = User.new(params[:user]) # often what you see in a controller action
user.valid? # checks to ensure your validations pass ... duh :)
user.save # saves the users
# from here, you can do a few things:
user.persisted? # this checks if the user was saved in the DB
user.id
# will now show you the user's id ...
# in your original post's code it appears you check for the id _before_ the save and it has to be saved before that attribute/method is populated.
The User Exists is Rails checking to see if that user is already in the DB before it tries to save it (my sense is that your User model validates the email and screen_name methods/attributes)
As for seeing the user after it's created you can do a couple of things:
If you're in a console, just type user to get the output.
In a web view (read: browser), you would go to http://localhost:3000/users/ID_OF_NEW_USER (this assumes you have a show action in your UsersController and a show view (show.html.erb or show.html.haml) ... and if you generated a scaffold (rails generate scaffold User email:string ..., you have these!)
Again, not totally sure I understood your request but I hope this helps you understand this flow a little better.
UPDATE:
Just saw the additions to your post. This code is incorrect:
user=User.new (:screen_name => "jeff holmes", :email "jeff92#jeff.com", :password "1234")
It should be
user=User.new (:screen_name => "jeff_holmes", :email => "jeff92#jeff.com", :password => "1234") # note the missing '=>' for email and password and the space in the screen_name, which is invalid according to the model

You are doing fine.
user = User.create name: "name", email: "email#test.com", password: "123456"
By calling the create method of a class its running all validations. Unless they failed its calling the save method. Since user is a new_object? rails will create the user.
After that you are having the user instance in your variable "user".
The other way is
user = User.new
user.email = "test#test.de"
....
user.save
Here again, the save method will call validations, and if they pass it will save the object to the database.
You can get the ID with user.id or User.last (which will load the latest user out of the database).
The best way to test those behaviors and play with it is the rails console. you can run it with rails c
you are doing fine.
just a little hint: have a look at the gem called "devise" which implements you a complete user-auth logic within 15 minues. no line of code needs to be written.

Related

ActiveStorage::IntegrityError Rspec attaching file

I have a factory for create dummy data, the factory name is migration_session.
So i create one instance of that factory by calling like this
#loan_migration = create(:migration_session, :loan, cooperative: #cooperative, management: #manager)
and this is my migration_session factory code
FactoryBot.define do
factory :migration_session, class: Migration::Session do
cooperative
management
trait :loan do
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
end
this is my migration_session model
class Migration::Session < ApplicationRecord
self.table_name = "migration_sessions"
has_one_attached :excel_file
end
because i call trait loan, it will attached excel_file attributes by excel file payment_migration.xlsx. but error like this comes after try to save the instance
ActiveStorage::IntegrityError:
ActiveStorage::IntegrityError
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:154:in `ensure_integrity_of'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:21:in `block in upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service.rb:126:in `instrument'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/service/disk_service.rb:19:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/app/models/active_storage/blob.rb:196:in `upload_without_unfurling'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/changes/create_one.rb:25:in `upload'
# /usr/local/bundle/gems/activestorage-6.0.3.4/lib/active_storage/attached/model.rb:56:in `block in has_one_attached'
# ./spec/requests/bulk_uploads_spec.rb:36:in `block (2 levels) in <top (required)>'
# /usr/local/bundle/gems/webmock-3.9.5/lib/webmock/rspec.rb:37:in `block (2 levels) in <top (required)>'
How to solve this problem? My file just fine, i can open and edit it.
I thing your problem is in the file path
excel_file { Rack::Test::UploadedFile.new("#{Rails.root}/public/payment_migration.xlsx", 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') }
Depending on your SO, the '/' char may not be allowed on path, so instead of pass the path string, let rails fill it for you.
Use the Rails.root.join method, like this:
Rails.root.join('public', 'payment_migration.xlsx')

Using a non-ActiveRecord model, can't access it through rails console

(Rails 4.2.4) Hello, beginner here. I am working on a project which does not need a DB or activeRecord. Therefore, when making my Rails project I appended the -O (to disable Active Record and database) (rails new MyApp -O)
I read that to do a model not backed by a database you can just create a file in
app/models/site.rb.
No need to do:
rails generate model Site
So I added my model, which looks something like this:
class Site
attr_reader :name
attr_reader :out_average
attr_reader :in_average
attr_reader :change
def initialize(name, in_average, out_average)
#name = name
#out_average = out_average
#in_average = in_average
#change = find_increase
end
def find_increase()
if #in_average && #out_average != 0
#change = ((#in_average - #out_average)/#out_average)*100
else
#change = 0
end
return #change
end
end
So, I then started up console "rails c" and when I try to invoke a new Site object, I get an error:
irb(main):001:0> Site.new
NameError: uninitialized constant Site
from (irb):1
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from /home/ms-87/Documents/projects/rails_projects/site_seasonality/bin/rails:8:in `<top (required)>'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /home/ms-87/.rbenv/versions/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from -e:1:in `<main>'
I made sure I started the console from the root of my app. I also made sure to use the proper naming convention (site.rb is the filename in app/model/, "Site" is the name of my class inside the file). Can anyone help me as to why this isn't working? Is my thinking here wrong? Thanks.
My first error was that my filenames were capitalized "Site.rb", I had actually fixed this before I posted. But after I fixed it, I accidentally started using "irb" instead of "rails c". DOH! Sorry for the post.

Rails before_destory only works with destroy and not with delete

I am building a rails applications, where in a part of it, I save to the database, payments information. I need to make these payments un-deleteable.
I have created a before_destroy function that kinda works.. but I am having an issue:
here is my code:
class StripePayment < ActiveRecord::Base
belongs_to :user
belongs_to :stripe_card
before_destroy :fail
private
def fail
return false
end
end
When I create a payment and try out my code when delete:
StripePayment.first.destroy returns false and rollback... Which is exactly what I want.
However,
StripePayment.first.delete passes and deletes the object.
I know the deference between delete and destroy. however, I want to be able to prevent this object from being deleted on the DB (on both delete() and destroy() calls.
I tried before_delete and rails gave me back this error:
NoMethodError: undefined method `before_delete' for #<Class:0x007fc1abc37c50>
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.0/lib/active_record/dynamic_matchers.rb:22:in `method_missing'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:7:in `<class:StripePayment>'
from /Users/alybadawy/developing/repos/finish/finish/app/models/stripe_payment.rb:1:in `<top (required)>'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:423:in `block in load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:615:in `new_constants_in'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:422:in `load_file'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:323:in `require_or_load'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:462:in `load_missing_constant'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:183:in `const_missing'
from (irb):1
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /Users/alybadawy/.rvm/gems/ruby-2.0.0-p247/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
Any help will be appreciated. Thanks :)
The easiest way is, to define your own delete method
class StripePayment < ActiveRecord::Base
belongs_to :user
belongs_to :stripe_card
before_destroy :fail
def delete
false
end
private
def fail
return false
end
end
If you have conditions, where you want to allow deleteion, you can check for it iy your delete method and call super
that's because delete is one of the functions that skips callbacks in rails
check this out for more info on the subject http://edgeguides.rubyonrails.org/active_record_callbacks.html#skipping-callbacks

undefined method `yaml' for nil:NilClass when trying to save duplicated object

I have an Active Record model object called Event, which has many event_things. I want to be able to duplicate the event, so that it gets a new id. I'm using Rails 3.2 and in rails console am able to successfully call
event_copy = event.dup
event_copy.save
However, I also want to duplicate each of the Event's event_things.
copy = event_thing.dup
copy.event_id = event_copy.id
copy.save
But that gives me this error stack:
NoMethodError: undefined method `yaml' for nil:NilClass
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych.rb:204:in `dump_stream'
from /Users/Ed/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/psych/core_ext.rb:35:in `psych_y'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:143:in `attribute_change'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `block in changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `map'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activemodel-3.2.6/lib/active_model/dirty.rb:117:in `changes'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/attribute_methods/dirty.rb:23:in `save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:208:in `transaction'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:241:in `block in save'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/activerecord-3.2.6/lib/active_record/transactions.rb:240:in `save'
from (irb):18
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:47:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands/console.rb:8:in `start'
from /Users/Ed/.rvm/gems/ruby-1.9.3-p125/gems/railties-3.2.6/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
I ran into the same issue as this. It turns out that there's a Kernel.y extension method that is getting in your way, based on this article. Here is what I did to fix my problem:
class Event < ActiveRecord::Base
# alias the attributes
alias_attribute :x_pos, :x
alias_attribute :y_pos, :y
after_initialize :init
# after_initialize will still work, with the aliases
def init
self.x_pos ||= 0
self.y_pos ||= 0
end
# override Kernel.y
def y; end
# override self.y_pos to read out of the :attributes, since we blew away :y
def y_pos
attribute['y']
end
end
After doing this, I was finally able to use my model without having to rename the y database column.
While I normally don't advocate using gems liberally, if you have a lot of relations that need to be preserved in the duplication, or find yourself doing these manual duplications a lot, take a look at deep_clonable.
As for your specific error, try surrounding each line of code with a nil check (one line at a time). I'm sure there's just some case where you're ending up with a nil object that you're overlooking.

Rails/Active Record FAILSAFE error "User can't be referred"

Using Rails 2.3.2 (not in a good situation to upgrade at the moment) with ruby 1.8.7 (2009-06-12 patchlevel 174) [universal-darwin10.0]. Getting the following error when trying to save when doing validations on the model, if the validations are :on => :update. If I change the validations to :on => :create and create a new record, I don't see the error, and I have no problem saving it if there are no validations.
Completed in 392ms (View: 10, DB: 296) | 200 OK [http://localhost/barfoos/update]
/!\ FAILSAFE /!\ Tue Sep 14 16:38:49 -0400 2010
Status: 500 Internal Server Error
User can't be referred
/path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:67:in `dump'
/path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:67:in `marshal'
/path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:123:in `marshal_data!'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `send'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:178:in `evaluate_method'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:166:in `call'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:93:in `run'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `each'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `send'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:92:in `run'
/path/to/project/vendor/rails/activesupport/lib/active_support/callbacks.rb:276:in `run_callbacks'
/path/to/project/vendor/rails/activerecord/lib/active_record/callbacks.rb:344:in `callback'
/path/to/project/vendor/rails/activerecord/lib/active_record/callbacks.rb:249:in `create_or_update'
/path/to/project/vendor/rails/activerecord/lib/active_record/base.rb:2539:in `save_without_validation'
/path/to/project/vendor/rails/activerecord/lib/active_record/validations.rb:1009:in `save_without_dirty'
/path/to/project/vendor/rails/activerecord/lib/active_record/dirty.rb:79:in `save_without_transactions'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `send'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:229:in `with_transaction_returning_status'
/path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in `transaction'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:182:in `transaction'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:228:in `with_transaction_returning_status'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save_without_unsaved_flag'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:208:in `rollback_active_record_state!'
/path/to/project/vendor/rails/activerecord/lib/active_record/transactions.rb:196:in `save_without_unsaved_flag'
/path/to/project/vendor/plugins/active_scaffold/lib/extensions/unsaved_record.rb:15:in `save'
/path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:300:in `set_session'
/path/to/project/vendor/rails/activerecord/lib/active_record/base.rb:1453:in `silence'
/path/to/project/vendor/rails/activerecord/lib/active_record/session_store.rb:297:in `set_session'
/path/to/project/vendor/rails/actionpack/lib/action_controller/session/abstract_store.rb:132:in `call'
/path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:29:in `call'
/path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:34:in `cache'
/path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:9:in `cache'
/path/to/project/vendor/rails/activerecord/lib/active_record/query_cache.rb:28:in `call'
/path/to/project/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in `call'
/path/to/project/vendor/rails/actionpack/lib/action_controller/reloader.rb:9:in `call'
/path/to/project/vendor/rails/actionpack/lib/action_controller/failsafe.rb:11:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `synchronize'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/lock.rb:11:in `call'
/path/to/project/vendor/rails/actionpack/lib/action_controller/dispatcher.rb:106:in `call'
/path/to/project/vendor/rails/railties/lib/rails/rack/static.rb:31:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:46:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `each'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/urlmap.rb:40:in `call'
/path/to/project/vendor/rails/railties/lib/rails/rack/log_tailer.rb:17:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/content_length.rb:13:in `call'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:50:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start'
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start'
/Library/Ruby/Gems/1.8/gems/rack-1.0.1/lib/rack/handler/webrick.rb:14:in `run'
/path/to/project/vendor/rails/railties/lib/commands/server.rb:111
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
/Library/Ruby/Site/1.8/rubygems/custom_require.rb:31:in `require'
script/server:3
The model looks like this:
class Barfoo < ActiveRecord::Base
default_scope :conditions => {:scoping_model_id => ScopingModel.current_version.id}
belongs_to :scoping_model
# validate is false to keep from attempting to validate Foobar on Barfoo save, as Foobar can be saved, even if invalid, unlike Barfoo.
has_one :foobar, :validate => false
validates_presence_of :foobar_id, :on => :create, :message => "can't be blank"
validates_inclusion_of :accepted_an_agreement, :in => [true, false], :on => :update, :message => "please choose whether you agree or disagree"
validates_presence_of :some_option_string, :on => :update, :message => "before agreeing, you must specify the some string", :if => Proc.new { |detail| detail.accepted_an_agreement == true }
validates_presence_of :some_text, :on => :update, :message => "you must provide details if option is 'Other'", :if => Proc.new { |detail| detail.some_option_string == 'Other' }
end
What I specify as the values of these fields doesn't matter, and as Shadwell notes in his comment below, it appears to be session-related. There is one thing session-related and user-related that we do in ApplicationController that could be causing this issue, even though it's never been a problem before now. ApplicationController looks like:
class ApplicationController < ActionController::Base
include ExceptionNotifiable
require 'something_that_defines_update_method_that_we_redefine_in_controller'
include Userstamp
ActionController::Base.session_options[:httponly] = true
ActionController::Base.session_options[:secure] = true
prepend_before_filter :user_setting_method
...
protected
def user_setting_method
session[:username] ||= optional_override_username
session[:username] = session[:username] # touch the session for timeout purposes
#user ||= User.find_by_username session[:username]
true
end
...
end
Search didn't bring up much about /!\ FAILSAFE /!\ with User can't be referred. Any idea about what would cause this and are there common solutions to this or things to look for that might be wrong?
Note that the foobar instance attached to this is saved, but in this particular case, foobar has previously been saved long before, but does not validate (was saved without validation, which is a normal possible state, since it is data from a multi-page form that saves the data partially and user can come back and correct later to finish).
After continuing to develop the model a bit more I started getting another error:
/!\ FAILSAFE /!\ Fri Sep 17 14:53:50 -0400 2010
Status: 500 Internal Server Error
can't dump hash with default proc
I tried saving the model in console and was successful, but saw some wierd things. It was asking for foobar_id to be valid (for example) when I'd already set a valid foobar on the model. So, I talked about it with a team member and heard that he had read about issues trying to validate on id of an associated object like that.
I removed:
validates_presence_of :foobar_id, :on => :create, :message => "can't be blank"
and it worked.
But, it was suggested to me by another coworker that the issues I was having were probably related to doing validates_presence_of :foobar_id, when, in the controller, I was attempting to set foobar:
barfoo.foobar = foobar
rather than setting foobar_id like:
barfoor.foobar_id = foobar.id
I also noticed that, with the former example, it wasn't setting foobar_id in the related row in the database.
After changing it to set foobar_id on barfoo, it seems to save on create and update as long as there is no update validation. However, if I add validation that validates :on => :update, it fails.
Im so late here. But I too had the same problem and I got a temporary fix for it
Just Override the marshal method in activerecord/lib/active_record/session_store.rb
ActiveRecord::SessionStore::ClassMethods.module_eval do
def marshal(data)
# To prevent from Marshal dump error
begin
::Base64.encode64(Marshal.dump(data)) if data
rescue
# Checks for any ActiveRecord object in session variables and reloads it
data.each_pair do |key,obj|
if obj.is_a?(ActiveRecord::Base) || obj.is_a?(Array)
unless obj.is_a?(Array)
data[key] = obj.reload
next
end
data[key].collect do |it_obj|
if obj.is_a?(ActiveRecord::Base)
it_obj.reload
else
it_obj
end
end
end
end
::Base64.encode64(Marshal.dump(data)) if data
end
end
end
In simple it will reload all the ActiveRecord objects present in the session variable

Resources