AASM4.1.1 can't work on Rails4 - ruby-on-rails

I have a model that contain a field called "status".I have change it into String type in DB.And I also use enum in AASM.
here is my code:
class Card < ActiveRecord::Base
include AASM
enum status: {
default: 0,
published: 1,
deleted: 2
}
aasm column: :status, enum: true, skip_validation_on_save: true, no_direct_assignment: true do
state :default, initial: true
state :published
state :deleted
event :publish do
transitions from: :default, to: :published
end
event :delete do
transitions from: :published, to: :deleted
end
end
end
And I apply it like this:
Card card = Card.first
card.publish
No matter I call card.publish or card.delete,I will get an error like this:
NoMethodError: undefined method `name' for nil:NilClass
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/core/state.rb:15:in `=='
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `block in state_object_for_name'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `each'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `find'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/instance_base.rb:61:in `state_object_for_name'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/aasm.rb:62:in `aasm_fire_event'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/persistence/active_record_persistence.rb:175:in `block in aasm_fire_event'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:in `within_new_transaction'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:in `transaction'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/persistence/active_record_persistence.rb:175:in `aasm_fire_event'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/aasm-4.1.1/lib/aasm/base.rb:81:in `block in event'
from (irb):13
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/console.rb:9:in `start'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/MadisonRong/.rvm/gems/ruby-2.0.0-p598/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'2.0.0-p598 :014 >
It puzzle me for a few days.Any help will be appreciate.

Shouldn't it be
enum status: [
:default,
:published,
:deleted
]
? If that doesn't help, please open open an issue on GitHub and I will take a closer look at it.

Related

How to dynamic create models with RoR and mongodb?

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.

First course table to add a field open, type Boolean;rake db:migrate Operation error?

class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean
change_column_default(:courses, :open, {:from=>true, :to=>false})
end
end
!!!!!then Operation!!!!!!
rake db:migrate
== 20161126133112 AddOpenAttributeToCourses: migrating ========================
-- add_column(:courses, :open, :boolean)
-> 0.1047s
-- change_column_default(:courses, :open, {:from=>true, :to=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
: ALTER TABLE "courses" ALTER COLUMN "open" SET DEFAULT '---
:from: true
:to: false
'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:455:in `change_column_default'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:665:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:654:in `method_missing'
/home/ubuntu/workspace/db/migrate/20161126133112_add_open_attribute_to_courses.rb:4:in `change'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:608:in `exec_migration'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:591:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:590:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:768:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:959:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:823:in `up'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:801:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
You're trying to use the Rails5 arguments to change_column_default:
change_column_default(table_name, column_name, default_or_changes)
[...]
Passing a hash containing :from and :to will make this change reversible in migration:
change_column_default(:posts, :state, from: nil, to: "draft")
with Rails4:
change_column_default(table_name, column_name, default)
Sets a new default value for a column: [...]
The funny looking stuff you see in the SQL:
---
:from: true
:to: false
is the YAML version of your { :from => true, :to => false } Ruby Hash BTW.
You need to use separate up and down methods and simpler version of change_column_default with Rails4:
def up
change_column_default(:courses, :open, false)
end
def down
change_column_default(:courses, :open, true)
end
or just set the default when you create the column:
class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean, :default => false
end
end
BTW, since you didn't specify a :default when you added the column it would have a default of null, not true.
You can create the column and set a default in one line:
add_column :courses, :open, :boolean, default: true
Not sure exactly why you're getting that error. Maybe it's because you say "change the default from false to true" but the default was not false, actually it was nil.

PgSearch 1.0.0 throws ModelNotMultisearchable error when trying to rebuild from console

I've got an app with 4 different models which are multisearchable, however they were originally set up with a pg_search_scope; ie:
class Episode < ActiveRecord::Base
include PgSearch
# multisearchable :against => [:title, :description]
pg_search_scope :search_text,
:against => [:title, :description],
:using => {
:tsearch => {:prefix => true}
}
As you can see at one point I tried setting this up the multiserachable way.
Of late there are items we know should be returning that are NOT in the search results, and I thought I'd have to rebuild the pg_search_documents; however that's throwing an error:
irb(main):004:0> PgSearch::Multisearch.rebuild(Show)
PgSearch::Multisearch::ModelNotMultisearchable: PgSearch::Multisearch::ModelNotMultisearchable
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch/rebuilder.rb:6:in `initialize'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:10:in `new'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:10:in `block in rebuild'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
from /app/vendor/bundle/ruby/2.0.0/gems/pg_search-0.7.3/lib/pg_search/multisearch.rb:8:in `rebuild'
from (irb):4
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /app/vendor/bundle/ruby/2.0.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from /app/bin/rails:4:in `require'
from /app/bin/rails:4:in `<main>'
irb(main):005:0>
I thought that it might be because of the setup as a pg_search_scope, so I set one class as above with the multisearch:multisearchable :against => [:title, :description] -- but even then I get rebuild problems.
I tried the manual way of deleting from the pg_search table and rebuilding but even THAT's throwing an error:
2.1.0 :010 > Show.find_each{ |record| record.update_pg_search_document }
Show Load (4.8ms) SELECT "shows".* FROM "shows" ORDER BY "shows"."id" ASC LIMIT 1000
NoMethodError: undefined method `update_pg_search_document' for #<Show:0x007fd945a92358>
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activemodel-4.0.2/lib/active_model/attribute_methods.rb:439:in `method_missing'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/attribute_methods.rb:155:in `method_missing'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/acts_as_follower-0.2.1/lib/acts_as_follower/followable.rb:54:in `method_missing'
from (irb):10:in `block in irb_binding'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `block (2 levels) in find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:26:in `block in find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:75:in `find_in_batches'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-deprecated_finders-1.0.3/lib/active_record/deprecated_finders/relation.rb:70:in `find_in_batches'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/relation/batches.rb:25:in `find_each'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/activerecord-4.0.2/lib/active_record/querying.rb:8:in `find_each'
from (irb):10
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:90:in `start'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands/console.rb:9:in `start'
from /Users/grimm/.rvm/gems/ruby-2.1.0/gems/railties-4.0.2/lib/rails/commands.rb:62:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4
I've been getting the same error while trying to rebuild (for the first time, installing) via the Rails command line with PgSearch::Multisearch.rebuild(Professor). What's worked for me is instead rebuilding from the command line using the alternative approach recommended on the Github:
rake pg_search:multisearch:rebuild[Professor]
This seems to work for me -- it doesn't throw an error, and when I go to the Rails console after and run PgSearch.multisearch("query") it's returning a result, whereas before it was returning an error.
I know this isn't a fix, but it's a solution to the immediate problem. I hope it helps -- posting an issue on their Github might lead to a fuller fix.

Rails 4 model is valid, but won't save?

I have the following models:
notification
class Notification < ActiveRecord::Base
belongs_to :notification_path
before_save :set_default
def set_default
self.resolved = false unless self.resolved
end
end
notification_path
class NotificationPath < ActiveRecord::Base
has_many :notifications
end
Then this code:
notification = Notification.new({"area"=>"test", "severity"=>"10", "message"=>"Test", "notification_path_id" => 3})
<Notification id: nil, area: "test", severity:
10, message: "Test", created_at: nil, updated_at: nil,
notification_path_id: 3, resolved: nil>
notification.valid?
true
notification.errors.full_messages
[]
So, as you can see - notification is valid and has no errors. There are zero validations on the notification model. But then, when I save the model:
notification.save
false
It won't save. What is causing this? It may be worth noting that set_default does run successfully, and even though the model isn't saving the resolved attribute is being set to false when it tries to save.
EDIT
Full error trace below, when following the above in IRB:
notification.save! ActiveRecord::RecordNotSaved: ActiveRecord::RecordNotSaved from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/persistence.rb:125:in
save!' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/validations.rb:57:in
save!' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/attribute_methods/dirty.rb:29:in
save!' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in
block in save!' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:329:in
block in with_transaction_returning_status' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
block in transaction' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:219:in
within_new_transaction' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
transaction' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:208:in
transaction' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:326:in
with_transaction_returning_status' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activerecord-4.1.0.rc1/lib/active_record/transactions.rb:273:in
save!' from (irb):23 from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:90:in
start' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/console.rb:9:in
start' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:69:in
console' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands/commands_tasks.rb:40:in
run_command!' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/railties-4.1.0.rc1/lib/rails/commands.rb:17:in' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in
require' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in
block in require' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in
load_dependency' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:247:in
require' from /Users/Jonathan/steel_notify/bin/rails:8:in <top
(required)>' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in
load' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in
block in load' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:232:in
load_dependency' from
/Users/Jonathan/.rvm/gems/ruby-2.1.1#steel_notify/gems/activesupport-4.1.0.rc1/lib/active_support/dependencies.rb:241:in
load' from
/Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from
/Users/Jonathan/.rvm/rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in
require' from -e:1:in'
In Ruby, assignments return the value being assigned. In your case you are setting self.resolved to false. When you return exactly false from a before_* callback, it will cancel the save operation. You need to return something that is not false after you make your assignment.

Rails giving problems with relational table, SQLite3::SQLException: no such column

Can't seem to find a solution to this one. I have a relational table, BlabsUser, from which I'm trying to find and destroy a record. I'm doing this:
BlabsUser.find_by_user_id_and_blab_id(1,29).destroy
And getting this lovely error:
BlabsUser Load (0.2ms) SELECT "blabs_users".* FROM "blabs_users" WHERE "blabs_users"."user_id" = 1 AND "blabs_users"."blab_id" = 29 LIMIT 1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:234:in `block in exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:223:in `exec_query'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:249:in `exec_delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/relation.rb:351:in `delete_all'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/persistence.rb:94:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/locking/optimistic.rb:119:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `block in destroy'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `destroy'
from (irb):1
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Why is it looking for a column called blabs_users? Why isn't it just looking for a record in my blabs_users table? Also, more importantly, how do I fix this?
Edit:
And I've also tried this with the same problem:
BlabsUser.destroy_all(:blab_id => 29, :user_id => 1)
Edit 2, as per request:
My model is just
class BlabsUser < ActiveRecord::Base
end
Migration is just
class BlabsUsers < ActiveRecord::Migration
def self.up
create_table :blabs_users, :id => false, :force => true do |t|
t.integer :user_id
t.integer :blab_id
t.timestamps
end
end
def self.down
drop_table :blabs_users
end
end
ActiveRecord depends pretty heavily on having a unique key to work with (usually the primary key of the table) Since you explicitly don't have one defined (:id => false), I strongly suspect that your problem is related to that.
destroy() is also documented as Destroy an object (or multiple objects) that has the given id which strongly suggests it needs that unique id to work with.

Resources