Let's say I had the email address like putin-crab#президент.рф
How to validate that address in rails 3.1?
My Model(i use Mongoid):
#encoding: utf-8
class User
include Mongoid::Document
field :email, :type => String
validates :email, :presence => true, :format => { :with => RFC822::EMAIL }
end
For validations reqexp i use gem https://github.com/dim/rfc-822
in rails console (normal email):
ruby-1.9.2-p290 :001 > usr = User.new( :email => "pretty#gmail.com" )
=> #<User _id: 4ec627cf4934db7e4d000001, _type: nil, email: "pretty#gmail.com">
ruby-1.9.2-p290 :002 > usr.valid?
=> true
in rails console (fu##ing email):
ruby-1.9.2-p290 :003 > usr = User.new( :email => "putin-crab#президент.рф" )
=> #<User _id: 4ec627f44934db7e4d000002, _type: nil, email: "putin-crab#президент.рф">
ruby-1.9.2-p290 :004 > usr.valid?
Encoding::CompatibilityError: incompatible encoding regexp match (ASCII-8BIT regexp with UTF-8 string)
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `=~'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `!~'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations/format.rb:9:in `validate_each'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:153:in `block in validate'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `each'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validator.rb:150:in `validate'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:302:in `_callback_before_13'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:404:in `_run_validate_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:42:in `block in run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `call'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `run_cascading_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:41:in `run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations.rb:212:in `run_validations!'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb:53:in `block in run_validations!'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:390:in `_run_validation_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activesupport-3.1.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:42:in `block in run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `call'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:67:in `run_cascading_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/callbacks.rb:41:in `run_callbacks'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations/callbacks.rb:53:in `run_validations!'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/activemodel-3.1.1/lib/active_model/validations.rb:179:in `valid?'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/mongoid-2.3.3/lib/mongoid/validations.rb:70:in `valid?'
from (irb):4
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/railties-3.1.1/lib/rails/commands/console.rb:45:in `start'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/railties-3.1.1/lib/rails/commands/console.rb:8:in `start'
from /home/username/.rvm/gems/ruby-1.9.2-p290#rail31/gems/railties-3.1.1/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
Emails cannot be recognized by Regexps because they are not a Regular language.
There are several good ways to validate an email address in Ruby (and Rails), each with various trade-offs:
If you only want to validate that it's the correct format, use a dedicated specific parser like the one from TMail, and see if the input is accepted (parsed successfully) (NOT A REGEXP). For example https://github.com/codyrobbins/active-model-email-validator
Once the email is known to have a correct format you can go a step further, and "talk to the internet" by doing DNS queries to see that the email's domain resolves and has mails servers (MX records) specified like https://github.com/Empact/validates_email_veracity_of although this might be too much.
Related
I'm trying to create a dynamic application to create any kind of data needed through metaprogramming the MVC i tried this for models:
class DynamicRecord
attr_accessor :name, :attributes
def initialize(name, attributes = [])
raise "Error: Constant #{name} already in namespace" if name.in? Object.constants
a_new_class = Class.new(Object) do |clazz|
include Mongoid::Document
attributes.map do |attribute|
field attribute[:name], type: attribute[:type]
end
end
Object.const_set(name, a_new_class)
end
end
DynamicRecord.new('Person', [{name: :name, type: String}, {name: :email, type: String}])
person = Person.new(name: "Foo", email: "Foo#foo.com")
person.save
Then I get this error:
Mongo::Error::OperationFailure: Invalid ns [mongodb_divcad_development.] (16257)
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/result.rb:256:in `validate!'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:60:in `block in execute_message'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server/connection_pool.rb:107:in `with_connection'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/server.rb:242:in `with_connection'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/insert.rb:59:in `execute_message'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/operation/write/write_command_enabled.rb:39:in `execute'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:365:in `block in insert_one'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/retryable.rb:112:in `write_with_retry'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongo-2.4.1/lib/mongo/collection.rb:356:in `insert_one'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/query_cache.rb:182:in `insert_one_with_clear_cache'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:79:in `insert_as_root'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:27:in `block in insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:118:in `block (2 levels) in prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__create__callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:117:in `block in prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:373:in `_run__4510143668266298615__save__callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/activesupport-4.0.2/lib/active_support/callbacks.rb:80:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/interceptable.rb:138:in `run_callbacks'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:116:in `prepare_insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/creatable.rb:23:in `insert'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/mongoid-5.2.0/lib/mongoid/persistable/savable.rb:23:in `save'
from (irb):70
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /home/cassiano/.rvm/gems/ruby-2.3.3/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
This can be done? There is a better approach without falling into a Entity-attribute-value model?
Mongoid doesn't know about namespace when you try to use dynamic model. In order to fix it you should set namespace with store_in
Example:
class DynamicCollection
def self.create(collection, fields)
klass = Class.new do
include Mongoid::Document
store_in collection: collection.downcase
fields.each do |item|
field item[:name], type: item[:type]
end
end
Object.const_set(collection, klass)
end
end
fields = [
{name: 'name', type: String},
{name: 'email', type: String}
]
DynamicCollection.create('Demo', fields)
Demo.create!(name: 'SomeValue', email: 'SomeValue')
I didn't try this locally, and this could be a dumb answer...but I would change
attributes.map to attributes.each seems like map would return an array and would cause an error.
Also...
https://github.com/mongodb/mongoid/blob/master/lib/mongoid/fields.rb#L335
Looking at the source for mongoid. There is a method called add_field that you maybe able to utilize.
I'm tying to test my Rails app with Rspec, but I'm getting a no implicit conversion of Symbol into Integer error without any apparent reason. Based on the traceback I get I think the problem is related to Mongo/Mongoid, however, I can't figure out what it is exactly. The code runs perfectly in production. The error happens only when testing.
Brief look at the model without the other methods:
class Card
include Mongoid::Document
field :front, type: String
field :back, type: String
field :level, type: Integer, default: 1
field :review_date, type: DateTime, default: DateTime.now
has_many :card_statistic, dependent: :destroy
belongs_to :topic
belongs_to :user
validates :front, :back, :level, presence: true
validates :topic, presence: { is: true, message: "must belong to a topic." }
validates :user, presence: { is: true, message: "must belong to a user." }
validates :level, numericality: { only_integer: true, greater_than: 0 }
end
One function in the model that triggers the error:
def self.reset(card)
card.update(level: 1)
end
The test code:
it "puts the given card in level 1" do
card = create(:card)
Card.correct card
card.reload
Card.correct card
card.reload
expect(card.level).to eq(3)
card.reset
card.reload
expect(card.level).to eq(1)
end
Then, the traceback of the error I get:
1) Card puts the given card in level 1
Failure/Error: Card.reset card
TypeError:
no implicit conversion of Symbol into Integer
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
# ./app/models/card.rb:57:in `reset'
# ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'
The error is also triggered when testing the controllers. Even doing a get :index throws the error. Thanks in advance for your help.
Apparently, there where some issues with the very last builds of Mongoid and I was pulling the code directly from the repo without specifying a stable version. So the solution was to either downgrade the version to 4.0 or use the 5.0 beta build explicitly.
Either
gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'
or
gem 'mongoid', '~> 4'
instead of
gem 'mongoid', '~> 5', github: 'mongoid/mongoid'
I have installed thinking sphinx and after running rake ts:index , it fails to configure the development file. The file is created, but it is empty.
Generating configuration to /Users/lexi87/dating/config/development.sphinx.conf
rake aborted!
undefined method `type' for nil:NilClass
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/type.rb:64:in `type_from_database'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/type.rb:17:in `type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute.rb:4:in `type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:30:in `sphinx_type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/attribute/sphinx_presenter.rb:18:in `collection_type'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:107:in `block in prepare_for_render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:104:in `each'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:104:in `prepare_for_render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/active_record/sql_source.rb:61:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `block in render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `collect'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration/index.rb:29:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/core/index.rb:48:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `block in render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `collect'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/riddle-1.5.6/lib/riddle/configuration.rb:39:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:81:in `render'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:87:in `block in render_to_file'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/configuration.rb:87:in `render_to_file'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/rake_interface.rb:4:in `configure'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/rake_interface.rb:31:in `index'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/gems/thinking-sphinx-3.0.0/lib/thinking_sphinx/tasks.rb:9:in `block (2 levels) in <top (required)>'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `eval'
/Users/lexi87/.rvm/gems/ruby-2.0.0-p0/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => ts:index
I am not sure if this is related to my user_index.rb under the indices folder.
ThinkingSphinx::Index.define :user, :with => :active_record do
# fields
indexes content
indexes user.name, :as => :user, :sortable => true
# attributes
has user_id, created_at, updated_at, ethnicity, religion
end
Here's my thinking_sphinx.yml file:
development:
mysql41: 9312
test:
mysql41: 9313
production:
mysql41: 9312
Any help would be greatly appreciated.
This error, while not clear, is due to one of your attribute references being not quite correct... If you're indexing the User model, then I'm guessing there isn't an association on it called user - so, user_id should just be id, and user.name should just be name:
ThinkingSphinx::Index.define :user, :with => :active_record do
# fields
indexes content
indexes name, :as => :user, :sortable => true
# attributes
has id, created_at, updated_at, ethnicity, religion
end
ok guys i have a problem - becouse i created a model and table AdminUser and then i trying to add values to it and then console print me ... any idea?
1.9.3-p286 :020 > AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :021 > AdminUser.all
AdminUser Load (0.2ms) SELECT "admin_user".* FROM "admin_user"
=> []
1.9.3-p286 :022 > me = AdminUser
=> AdminUser(id: integer, first_name: string, last_name: string, email: string, pass: string, date_reg: datetime, update: datetime)
1.9.3-p286 :023 > me.new
ActiveRecord::DangerousAttributeError: update is defined by ActiveRecord
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:91:in `instance_method_already_implemented?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:263:in `block in define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:260:in `define_attribute_method'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `block in define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `each'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:256:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:66:in `block in define_attribute_methods'
from <internal:prelude>:10:in `synchronize'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:63:in `define_attribute_methods'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:168:in `respond_to?'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:398:in `__run_callback'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_initialize_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/activerecord-3.2.8/lib/active_record/base.rb:501:in `initialize'
from (irb):23:in `new'
from (irb):23
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /home/wojtek/.rvm/gems/ruby-1.9.3-p286/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'1.9.3-p286 :024 >
Your problem is that update is a column in your model. But update is also a method defined by the Active Record. So rails won't know which your talking about if you try and access admin.update. You should be fine if you rename your update column to something else.
You might want to look at timestamps. http://guides.rubyonrails.org/migrations.html#special-helpers it looks to me like what you want to do with the update column.
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