I have the following two models:
# app/models/customer.rb
class Customer < ActiveRecord::Base
has_paper_trail
serialize :mail_opt_out, Set
before_create :generate_token
has_many :wallets
has_many :tickets, through: :wallets
...
# We have a special seeded customer with id -1 that we don't want changing
def readonly?
persisted? && id < 0
end
end
# app/models/ticket.rb
class Ticket < ActiveRecord::Base
include SparkCast
has_paper_trail
belongs_to :price
belongs_to :basket
belongs_to :occurrence
has_one :event, through: :occurrence
has_one :wallet, through: :basket
has_one :basket_type, through: :basket
has_one :customer, through: :basket
delegate :id, to: :customer, allow_nil: true
...
def admit
ensure_can_admit
self.state = 'admitted'
self.save!
end
cast_on :admit, room: :admittance
def as_cast
{
id: id,
customer_id: customer_id
}
end
end
The relevant association is a little convoluted, but is customers -> wallets -> baskets -> tickets.
When I call admit on an instance of a ticket that belongs to our customer with id -1, (which is read only) I get an ActiveRecord::ReadOnlyRecord exception.
I'm confused as to what is causing this, as neither ticket nor customer have any before_update callbacks. If I change
has_one :customer, through: :basket
to
delegate :customer, to: :basket
then everything is fine. Something seems to be either trying to update the customer, or at least checking if it is readonly.
I've done a bit of stepping through the save procedure using byebug, but haven't been able to find anything useful.
What is likely to be checking if an associated model is readonly, and how do I get around this? Is using delegate the best option here?
Edited to add:
Also including SparkCast, which I had omitted previously. Removing the cast_on method from my ticket model is fixing the problem.
# app/models/concerns/spark_cast.rb
module SparkCast
extend ActiveSupport::Concern
module ClassMethods
def cast_on(*args)
# Options are the last argument.
options = args.pop
room = options[:room]
args.each do |operation|
class_eval do
begin
alias_method "#{operation}_without_cast", operation
define_method operation do |*args|
cast_hash = as_cast
...
send("#{operation}_without_cast", *args)
cast(room, operation, cast_hash)
end
rescue NameError => e
raise "#{e.name} has not been defined yet. Include cast_on after method definition"
end
end
end
end
end
end
Backtrace:
- activerecord (4.1.0) lib/active_record/persistence.rb:481:in `create_or_update'
- activerecord (4.1.0) lib/active_record/callbacks.rb:302:in `block in create_or_update'
- activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
- activerecord (4.1.0) lib/active_record/callbacks.rb:302:in `create_or_update'
- activerecord (4.1.0) lib/active_record/persistence.rb:103:in `save'
- activerecord (4.1.0) lib/active_record/validations.rb:51:in `save'
- activerecord (4.1.0) lib/active_record/attribute_methods/dirty.rb:21:in `save'
- activerecord (4.1.0) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
- activerecord (4.1.0) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
- activerecord (4.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `transaction'
- activerecord (4.1.0) lib/active_record/transactions.rb:208:in `transaction'
- activerecord (4.1.0) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
- activerecord (4.1.0) lib/active_record/transactions.rb:268:in `block in save'
- activerecord (4.1.0) lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
- activerecord (4.1.0) lib/active_record/transactions.rb:267:in `save'
- activerecord (4.1.0) lib/active_record/autosave_association.rb:393:in `save_has_one_association'
- activerecord (4.1.0) lib/active_record/autosave_association.rb:188:in `block in add_autosave_association_callbacks'
- activesupport (4.1.0) lib/active_support/callbacks.rb:424:in `block in make_lambda'
- activesupport (4.1.0) lib/active_support/callbacks.rb:221:in `block in halting_and_conditional'
- activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
- activerecord (4.1.0) lib/active_record/callbacks.rb:310:in `update_record'
- activerecord (4.1.0) lib/active_record/timestamp.rb:70:in `update_record'
- activerecord (4.1.0) lib/active_record/persistence.rb:482:in `create_or_update'
- activerecord (4.1.0) lib/active_record/callbacks.rb:302:in `block in create_or_update'
- activesupport (4.1.0) lib/active_support/callbacks.rb:113:in `call'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:166:in `block in halting'
- activesupport (4.1.0) lib/active_support/callbacks.rb:86:in `run_callbacks'
- activerecord (4.1.0) lib/active_record/callbacks.rb:302:in `create_or_update'
- activerecord (4.1.0) lib/active_record/persistence.rb:125:in `save!'
- activerecord (4.1.0) lib/active_record/validations.rb:57:in `save!'
- activerecord (4.1.0) lib/active_record/attribute_methods/dirty.rb:29:in `save!'
- activerecord (4.1.0) lib/active_record/transactions.rb:273:in `block in save!'
- activerecord (4.1.0) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
- activerecord (4.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `block in transaction'
- activerecord (4.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:219:in `within_new_transaction'
- activerecord (4.1.0) lib/active_record/connection_adapters/abstract/database_statements.rb:211:in `transaction'
- activerecord (4.1.0) lib/active_record/transactions.rb:208:in `transaction'
- activerecord (4.1.0) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
- activerecord (4.1.0) lib/active_record/transactions.rb:273:in `save!'
- () home/hayden/development/SparkSeat/api/app/models/ticket.rb:79:in `admit'
Related
I'm writing a Application which has following Model Design.
Model A has_many bs
Model B belongs_to a
b is sigular an bs/BS is plural
a is singular and as/AS is plural
and I'm using an Admin-controller to define what the users can do
class Admin::BsController < ApplicationController
def create
#a = A.find(params[:a_id])
#b = #a.bs.create(entry_params)
redirect_to a_path(#a)
end
def new
#b = B.new
end
end
The form in the view is show.html.erb is generated like
<%= form_with(model: [:admin, #a,B.new ], local: true) do |f| %>
<%= end %>
Expected behavior
Tell us what should happen
The new DB-entry which belongs_to a should be created.
I get an error
NoMethodError in Admin::EntriesController#create
undefined method `string' for #<B:0x00007f93a8090450> Did you mean? String
Rails version: Rails 5.1.5
Ruby version: Ruby 2.5.0p0
UPDATE
The idea is that a user can create a list with an personalized ID (admin_key) like doodle
Model A is the list
Model B are the entries of A
And can share that entry with the public_key)
That runs!
The problem is to add new entries to the list
The admin controlls that only the owner-url can run CRUD function to the list
The schema file
ActiveRecord::Schema.define(version: 20161023094929) do
create_table "admin_lists", force: :cascade do |t|
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "entries", force: :cascade do |t|
t.string "title"
t.string "email"
t.string "description"
t.boolean "state"
t.integer "list_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["list_id"], name: "index_entries_on_list_id"
end
create_table "lists", force: :cascade do |t|
t.string "title"
t.text "description"
t.string "email"
t.string "admin_key"
t.string "public_key"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "exp_date"
t.string "state"
end
end
The full stackstrace
activemodel (5.1.5) lib/active_model/attribute_methods.rb:432:in `method_missing'
activemodel (5.1.5) lib/active_model/validator.rb:148:in `block in validate'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `each'
activemodel (5.1.5) lib/active_model/validator.rb:147:in `validate'
activesupport (5.1.5) lib/active_support/callbacks.rb:413:in `block in make_lambda'
activesupport (5.1.5) lib/active_support/callbacks.rb:197:in `block (2 levels) in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:601:in `block (2 levels) in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `catch'
activesupport (5.1.5) lib/active_support/callbacks.rb:600:in `block in default_terminator'
activesupport (5.1.5) lib/active_support/callbacks.rb:198:in `block in halting'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.5) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.5) lib/active_support/callbacks.rb:130:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.5) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
activesupport (5.1.5) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.5) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.5) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.5) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.1.5) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.1.5) lib/active_record/validations.rb:44:in `save'
activerecord (5.1.5) lib/active_record/attribute_methods/dirty.rb:35:in `save'
activerecord (5.1.5) lib/active_record/transactions.rb:308:in `block (2 levels) in save'
activerecord (5.1.5) lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
activerecord (5.1.5) lib/active_record/transactions.rb:308:in `block in save'
activerecord (5.1.5) lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
activerecord (5.1.5) lib/active_record/transactions.rb:307:in `save'
activerecord (5.1.5) lib/active_record/suppressor.rb:42:in `save'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:371:in `insert_record'
activerecord (5.1.5) lib/active_record/associations/has_many_association.rb:34:in `insert_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:360:in `block (2 levels) in _create_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:447:in `replace_on_target'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:281:in `add_to_target'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:129:in `block in transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/home/marcus/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
activerecord (5.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
activerecord (5.1.5) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:128:in `transaction'
activerecord (5.1.5) lib/active_record/associations/collection_association.rb:357:in `_create_record'
activerecord (5.1.5) lib/active_record/associations/has_many_association.rb:121:in `_create_record'
activerecord (5.1.5) lib/active_record/associations/association.rb:196:in `create'
activerecord (5.1.5) lib/active_record/associations/collection_proxy.rb:347:in `create'
app/controllers/admin/entries_controller.rb:7:in `create'
actionpack (5.1.5) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.5) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.5) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.5) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.5) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.5) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.5) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.5) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.5) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.5) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.5) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.5) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.5) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.5) lib/action_dispatch/routing/route_set.rb:844:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
rack (2.0.4) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.4) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.5) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.5) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.5) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
web-console (3.5.1) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.5.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `catch'
web-console (3.5.1) lib/web_console/middleware.rb:18:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.5) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.5) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.5) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.5) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
railties (5.1.5) lib/rails/engine.rb:522:in `call'
puma (3.11.3) lib/puma/configuration.rb:225:in `call'
puma (3.11.3) lib/puma/server.rb:624:in `handle_request'
puma (3.11.3) lib/puma/server.rb:438:in `process_client'
puma (3.11.3) lib/puma/server.rb:302:in `block in run'
puma (3.11.3) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
models/wishlist.rb
#models/list.rb
class List < ApplicationRecord
has_many :entries
validates :email, presence: true
validates :admin_key, :uniqueness => true
before_create :create_admin_key
before_create :create_public_key
state_machine initial: :inactive do
event :activate do
transition :inactive => :active
end
event :deactivate do
transition :activate => :inactive
end
end
def create_admin_key
begin
self.admin_key = SecureRandom.urlsafe_base64(len=15)
end while self.class.exists?(admin_key: admin_key)
end
def create_public_key
begin
self.public_key = SecureRandom.urlsafe_base64(len=8)
end while self.class.exists?(public_key: public_key)
end
# defined the public_key as the link to navigate the wishlist
def to_param
admin_key
end
end
models/entry.rb
#models/entry.rb
class Entry < ApplicationRecord
belongs_to :wishlist, optional: true
*validates :string, :uniqueness => true
before_create :create_entry_key
def create_entry_key
begin
self.string = SecureRandom.urlsafe_base64(len=5)
* end while self.class.exists?(string: string)
end
end
I hope this help you. I dont't find the error.
The Bug is in the Model entry.rb. The valiation is the model is a template. here is the correct code.
The broken and fixed code is marked with an arrow. This was in question only string and no variables. In my case entry_key.
#models/entry.rb
class Entry < ApplicationRecord
belongs_to :wishlist, optional: true
before_create :create_entry_key
validates_presence_of :wishlist
validates :entry_key, :uniqueness => true # <-
def create_entry_key
begin
self.entry_key = SecureRandom.urlsafe_base64(len=5) # <-
end while self.class.exists?(entry_key: entry_key) # <-
end
end
i'm having troubles with Devise Gem, i'm using the default recoverable methods, the links is being sent. The reset values (reset_password_sent_at) is saved in the database, but when i have to write down the new password and update it, gives this error
NoMethodError (undefined method `to_datetime' for nil:NilClass):
activesupport (4.0.2) lib/active_support/core_ext/date_time/calculations.rb:161:in `<=>'
activesupport (4.0.2) lib/active_support/core_ext/time/calculations.rb:286:in `compare_with_coercion'
activesupport (4.0.2) lib/active_support/time_with_zone.rb:214:in `<=>'
activerecord (4.0.2) lib/active_record/attribute_methods/dirty.rb:97:in `=='
activerecord (4.0.2) lib/active_record/attribute_methods/dirty.rb:97:in `!='
activerecord (4.0.2) lib/active_record/attribute_methods/dirty.rb:97:in `_field_changed?'
activerecord (4.0.2) lib/active_record/attribute_methods/dirty.rb:66:in `write_attribute'
activerecord (4.0.2) lib/active_record/attribute_methods/time_zone_conversion.rb:39:in `reset_password_sent_at='
devise (3.5.6) lib/devise/models/recoverable.rb:94:in `clear_reset_password_token'
devise (3.5.6) lib/devise/models/recoverable.rb:32:in `block (2 levels) in <module:Recoverable>'
activesupport (4.0.2) lib/active_support/callbacks.rb:377:in `_run__1626105923374900797__update__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
activerecord (4.0.2) lib/active_record/callbacks.rb:310:in `update_record'
activerecord (4.0.2) lib/active_record/timestamp.rb:70:in `update_record'
activerecord (4.0.2) lib/active_record/persistence.rb:477:in `create_or_update'
activerecord (4.0.2) lib/active_record/callbacks.rb:302:in `block in create_or_update'
activesupport (4.0.2) lib/active_support/callbacks.rb:393:in `_run__1626105923374900797__save__callbacks'
activesupport (4.0.2) lib/active_support/callbacks.rb:80:in `run_callbacks'
activerecord (4.0.2) lib/active_record/callbacks.rb:302:in `create_or_update'
activerecord (4.0.2) lib/active_record/persistence.rb:106:in `save'
activerecord (4.0.2) lib/active_record/validations.rb:51:in `save'
activerecord (4.0.2) lib/active_record/attribute_methods/dirty.rb:32:in `save'
activerecord (4.0.2) lib/active_record/transactions.rb:270:in `block (2 levels) in save'
activerecord (4.0.2) lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
activerecord (4.0.2) lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
activerecord (4.0.2) lib/active_record/transactions.rb:209:in `transaction'
activerecord (4.0.2) lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
activerecord (4.0.2) lib/active_record/transactions.rb:270:in `block in save'
activerecord (4.0.2) lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
activerecord (4.0.2) lib/active_record/transactions.rb:269:in `save'
devise (3.5.6) lib/devise/models/recoverable.rb:48:in `reset_password'
devise (3.5.6) lib/devise/models/recoverable.rb:141:in `reset_password_by_token'
devise (3.5.6) app/controllers/devise/passwords_controller.rb:32:in `update'
debugging this its seems to be a problem with the reset_password_sent_at being nil when the password is reset (lib/devise/models/recoverable.rb:94:inclear_reset_password_token)
I've try to change my local time to the UTC stored but the problem isn't that. Any suggestion?
My solution for this was to upgrade my rails from 4.0.2 to 4.2.7. When i run the bundle install and update the gemfile dependencies now is working.
Upgrading devise from (3.5.6) to (4.2.0)
I've got a callback and method defined in the parent class of a Rails STI setup.
class Parent < ActiveRecord::Base
before_save :populate_name
# implicitly public
def populate_name
self.name = "foobar"
end
class Child < Parent
end
When I make populate_name private or protected like this:
class Parent < ActiveRecord::Base
before_save :populate_name
private
def populate_name
self.name = "foobar"
end
class Child < Parent
end
Then I get this error:
NameError - undefined local variable or method `populate_name' for #<Child:0x007ff901eace30>:
activemodel (3.2.16) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.16) lib/active_record/attribute_methods.rb:149:in `method_missing'
activesupport (3.2.16) lib/active_support/callbacks.rb:407:in `_run__3119425560225797910__save__1411052685854526397__callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.16) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.16) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.16) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.16) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.16) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.16) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
activerecord (3.2.16) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.16) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.16) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.16) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.16) lib/active_record/transactions.rb:259:in `block in save'
activerecord (3.2.16) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
activerecord (3.2.16) lib/active_record/transactions.rb:258:in `save'
app/controllers/child_controller.rb:135:in `create_or_update'
Is public required for the callback?
Private methods can't be accessed by children. Protected methods can. The child doesn't have access to the private method.
When should we consider using private or protected?
Keeping this question up in case anybody ever googles this (and it's a fat finger thing).
I had accidently put the private part after the end for the class before the module end. It's easy to just throw the private, then private method before the last end, but one really needs to be careful if there's nesting!
So yes
make the callbacks private
if you don't see something in a google query for something simple, you must have fat fingered something
module Foo
class Parent < ActiveRecord::Base
before_save :populate_name
end
private
def populate_name
self.name = "foobar"
end
end
class Child < Parent
end
Then I get this error:
NameError - undefined local variable or method `populate_name' for #<Child:0x007ff901eace30>:
activemodel (3.2.16) lib/active_model/attribute_methods.rb:407:in `method_missing'
activerecord (3.2.16) lib/active_record/attribute_methods.rb:149:in `method_missing'
activesupport (3.2.16) lib/active_support/callbacks.rb:407:in `_run__3119425560225797910__save__1411052685854526397__callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:405:in `__run_callback'
activesupport (3.2.16) lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
activesupport (3.2.16) lib/active_support/callbacks.rb:81:in `run_callbacks'
activerecord (3.2.16) lib/active_record/callbacks.rb:264:in `create_or_update'
activerecord (3.2.16) lib/active_record/persistence.rb:84:in `save'
activerecord (3.2.16) lib/active_record/validations.rb:50:in `save'
activerecord (3.2.16) lib/active_record/attribute_methods/dirty.rb:22:in `save'
activerecord (3.2.16) lib/active_record/transactions.rb:259:in `block (2 levels) in save'
activerecord (3.2.16) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
activerecord (3.2.16) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
activerecord (3.2.16) lib/active_record/transactions.rb:208:in `transaction'
activerecord (3.2.16) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
activerecord (3.2.16) lib/active_record/transactions.rb:259:in `block in save'
activerecord (3.2.16) lib/active_record/transactions.rb:270:in `rollback_active_record_state!'
activerecord (3.2.16) lib/active_record/transactions.rb:258:in `save'
app/controllers/child_controller.rb:135:in `create_or_update'
I have the following relationship in my models:
class ClientProfile < ActiveRecord::Base
has_many :client_folders
has_many :folders, through: :client_folders
accepts_nested_attributes_for :folders
end
class Folder < ActiveRecord::Base
has_many :client_folders
has_many :client_profiles, through: :client_folders
end
class ClientFolder < ActiveRecord::Base
belongs_to :client_profile
belongs_to :folder
end
I have the relationships built up in controller:
#client_profile = ClientProfile.new
2.times do |i|
#folder = Folder.new(folder_name: "folder #{i}")
#client_profile.folders << #folder
end
I have the following fields_for associations created in view:
<%= form_for #client_profile do |f| %>
...
<%= f.fields_for :folders do |folder_builder| %>
<%= folder_builder.text_field :some_column %>
...
<% end %>
<% end %>
And the create action:
def create
#client_profile = ClientProfile.new client_profile_params
if #client_profile.save
...
else
...
end
end
When I save the association, it does create the client_profile as well as the two folders, and the join model ClientFolder is correctly created twice. However in both ClientFolders created, it only has the folder_id filled in. The client_profile_id is left null.
One solution I tried is adding the following to client_profile to ensure it saves the join model relation correctly:
def folders_attributes=(params)
if params["0"][:id].nil?
params.values.each do |v|
f = Folder.new v
self.folders << f
end
end
end
But this raises the following exception when saving:
NoMethodError - undefined method `each' for #<ClientFolder:0x007fd364743d78>:
activemodel (4.1.5) lib/active_model/attribute_methods.rb:435:in `method_missing'
activerecord (4.1.5) lib/active_record/attribute_methods.rb:208:in `method_missing'
activerecord (4.1.5) lib/active_record/autosave_association.rb:349:in `save_collection_association'
activerecord (4.1.5) lib/active_record/autosave_association.rb:186:in `block in add_autosave_association_callbacks'
activerecord (4.1.5) lib/active_record/autosave_association.rb:157:in `instance_eval'
activerecord (4.1.5) lib/active_record/autosave_association.rb:157:in `block in define_non_cyclic_method'
activesupport (4.1.5) lib/active_support/callbacks.rb:424:in `block in make_lambda'
activesupport (4.1.5) lib/active_support/callbacks.rb:221:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:221:in `block in halting_and_conditional'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:215:in `block in halting_and_conditional'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
activerecord (4.1.5) lib/active_record/callbacks.rb:306:in `_create_record'
activerecord (4.1.5) lib/active_record/timestamp.rb:57:in `_create_record'
activerecord (4.1.5) lib/active_record/persistence.rb:482:in `create_or_update'
activerecord (4.1.5) lib/active_record/callbacks.rb:302:in `block in create_or_update'
activesupport (4.1.5) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:113:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:166:in `block in halting'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `call'
activesupport (4.1.5) lib/active_support/callbacks.rb:86:in `run_callbacks'
activerecord (4.1.5) lib/active_record/callbacks.rb:302:in `create_or_update'
activerecord (4.1.5) lib/active_record/persistence.rb:103:in `save'
activerecord (4.1.5) lib/active_record/validations.rb:51:in `save'
activerecord (4.1.5) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (4.1.5) lib/active_record/transactions.rb:268:in `block (2 levels) in save'
activerecord (4.1.5) lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
activerecord (4.1.5) lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
activerecord (4.1.5) lib/active_record/transactions.rb:208:in `transaction'
activerecord (4.1.5) lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
activerecord (4.1.5) lib/active_record/transactions.rb:268:in `block in save'
Someone else recommended to add the join model association:
accepts_nested_attributes_for :client_folders
But it doesn't make sense to do that, since I am not using client_folders at all in the form. When I use fields_for :folders in form, it should be intelligent enough to save the join model properly in create.
How can I resolve this issue?
I discovered the problem.
I had defined the relation twice in the model:
has_many :client_folders
has_one :client_folders, :class_name => 'ClientFolder', :foreign_key => :folder_id
When I removed that has_one, everything worked accordingly.
I am trying to upgrade my application from Rails 2.3.5 to Rails 3.0.3.
Models:
Project.rb
has_many :project_roles, :dependent => :destroy
alias :roles :project_roles
after_create :create_cal_for_project, :make_project_owner
def create_calendar_for_project
self.calendars.create(:name => "default")
end
def make_project_owner
#Make owner of project when created
self.roles.create(:user => User.curr_user, :name => 'O')
end
ProjectRole.rb
belongs_to :project
belongs_to :user
project_roles table columns are user_id, project_id, name
I am getting a hard time with the following error, please suggest.
Thanks.
→ rails c
> Loading development environment (Rails 3.0.3)
ruby-1.9.2-p0> User.curr_user = User.first
=> #<User id: 1, username: "tispratik", login_email: "tispratik#gmail.com", > is_email_verified: nil, crypted_password: "deed6fa27ffefa57e63592a9b59295abf2660447cf281f34857...", password_salt: "YsD2nLte3pZy1FZ78GD", persistence_token: "84e44908be77b2dc1e41f8dfeacf9ef20c30050ff0c53854e0d...", single_access_token: "cp1bqVSheDh7vl9J48V", perishable_token: "6pGPzdw48nJnJAHsIZQW", last_login_ip: nil, last_login_at: nil, created_at: "2010-11-23 06:00:09", updated_at: "2010-11-23 06:00:09">
ruby-1.9.2-p0> p = Project.create(:name => "ad", :description => "add")
NoMethodError: undefined method `name' for nil:NilClass
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:57:in `block in visit_Arel_Nodes_InsertStatement'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:56:in `map'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:56:in `visit_Arel_Nodes_InsertStatement'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:15:in `visit'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/visitor.rb:5:in `accept'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:19:in `block in accept'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/connection_pool.rb:110:in `with_connection'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/visitors/to_sql.rb:17:in `accept'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/tree_manager.rb:19:in `to_sql'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/arel-2.0.4/lib/arel/select_manager.rb:191:in `insert'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/relation.rb:14:in `insert'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/persistence.rb:270:in `create'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/timestamp.rb:47:in `create'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:281:in `block in create'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:413:in `_run_create_callbacks'
... 30 levels...
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:21:in `save'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block (2 levels) in save'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block in save'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:236:in `save'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:498:in `create'
from (irb):2
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /home/pratik/.rvm/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The problem is in the make_project_owner callback. create_calendar_for_project runs just fine.
I changed make_project_owner to the following still the same problem occurs.
def make_project_owner
#Make owner of project when created
#role.create(:user => User.curr_user, :name => 'O')
ProjectRole.create(:user => User.curr_user, :project => self, :name => "O")
end
I tried downgrading rails:
rails 3.0.2 -> has same problem
rails 3.0.1 -> has different error
undefined method `name' for #<Arel::Value:0x00000005795090>
Application Trace | Framework Trace | Full Trace
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `block in create'
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `each'
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `detect'
arel (1.0.1) lib/arel/engines/sql/engine.rb:26:in `create'
arel (1.0.1) lib/arel/algebra/relations/writes.rb:24:in `call'
arel (1.0.1) lib/arel/session.rb:17:in `create'
arel (1.0.1) lib/arel/algebra/relations/relation.rb:159:in `insert'
activerecord (3.0.1) lib/active_record/relation.rb:14:in `insert'
activerecord (3.0.1) lib/active_record/persistence.rb:271:in `create'
activerecord (3.0.1) lib/active_record/timestamp.rb:47:in `create'
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `block in create'
activesupport (3.0.1) lib/active_support/callbacks.rb:413:in `_run_create_callbacks'
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `create'
activerecord (3.0.1) lib/active_record/persistence.rb:247:in `create_or_update'
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `block in create_or_update'
activesupport (3.0.1) lib/active_support/callbacks.rb:423:in `_run_save_callbacks'
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `create_or_update'
activerecord (3.0.1) lib/active_record/persistence.rb:39:in `save'
activerecord (3.0.1) lib/active_record/validations.rb:43:in `save'
activerecord (3.0.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block (2 levels) in save'
activerecord (3.0.1) lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.1) lib/active_record/transactions.rb:204:in `transaction'
activerecord (3.0.1) lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block in save'
activerecord (3.0.1) lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
activerecord (3.0.1) lib/active_record/transactions.rb:236:in `save'
activerecord (3.0.1) lib/active_record/base.rb:498:in `create'
app/models/project.rb:70:in `make_project_owner'
activesupport (3.0.1) lib/active_support/callbacks.rb:463:in `_run_create_callbacks'
activerecord (3.0.1) lib/active_record/callbacks.rb:281:in `create'
activerecord (3.0.1) lib/active_record/persistence.rb:247:in `create_or_update'
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `block in create_or_update'
activesupport (3.0.1) lib/active_support/callbacks.rb:428:in `_run_save_callbacks'
activerecord (3.0.1) lib/active_record/callbacks.rb:277:in `create_or_update'
activerecord (3.0.1) lib/active_record/persistence.rb:39:in `save'
activerecord (3.0.1) lib/active_record/validations.rb:43:in `save'
activerecord (3.0.1) lib/active_record/attribute_methods/dirty.rb:21:in `save'
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block (2 levels) in save'
activerecord (3.0.1) lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.1) lib/active_record/transactions.rb:204:in `transaction'
activerecord (3.0.1) lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
activerecord (3.0.1) lib/active_record/transactions.rb:237:in `block in save'
activerecord (3.0.1) lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
activerecord (3.0.1) lib/active_record/transactions.rb:236:in `save'
app/controllers/projects_controller.rb:34:in `create'
actionpack (3.0.1) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:150:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
activesupport (3.0.1) lib/active_support/callbacks.rb:460:in `_run__2176748434120211400__process_action__1031157339911956458__callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.1) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.1) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `block in instrument'
activesupport (3.0.1) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (3.0.1) lib/active_support/notifications.rb:52:in `instrument'
actionpack (3.0.1) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
actionpack (3.0.1) lib/action_controller/metal/rescue.rb:17:in `process_action'
actionpack (3.0.1) lib/abstract_controller/base.rb:119:in `process'
actionpack (3.0.1) lib/abstract_controller/rendering.rb:40:in `process'
actionpack (3.0.1) lib/action_controller/metal.rb:133:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
actionpack (3.0.1) lib/action_controller/metal.rb:173:in `block in action'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:62:in `dispatch'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:27:in `call'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:75:in `optimized_each'
rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize'
rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call'
actionpack (3.0.1) lib/action_dispatch/routing/route_set.rb:492:in `call'
haml (3.0.24) lib/sass/plugin/rack.rb:41:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/head.rb:14:in `call'
rack (1.2.1) lib/rack/methodoverride.rb:24:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/flash.rb:182:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/cookies.rb:287:in `call'
activerecord (3.0.1) lib/active_record/query_cache.rb:32:in `block in call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:12:in `cache'
activerecord (3.0.1) lib/active_record/query_cache.rb:31:in `call'
activerecord (3.0.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call'
activesupport (3.0.1) lib/active_support/callbacks.rb:415:in `_run_call_callbacks'
actionpack (3.0.1) lib/action_dispatch/middleware/callbacks.rb:44:in `call'
rack (1.2.1) lib/rack/sendfile.rb:107:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/remote_ip.rb:48:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call'
railties (3.0.1) lib/rails/rack/logger.rb:13:in `call'
rack (1.2.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.0.1) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.2.1) lib/rack/lock.rb:11:in `block in call'
<internal:prelude>:10:in `synchronize'
rack (1.2.1) lib/rack/lock.rb:11:in `call'
actionpack (3.0.1) lib/action_dispatch/middleware/static.rb:30:in `call'
railties (3.0.1) lib/rails/application.rb:168:in `call'
railties (3.0.1) lib/rails/application.rb:77:in `method_missing'
railties (3.0.1) lib/rails/rack/log_tailer.rb:14:in `call'
rack (1.2.1) lib/rack/content_length.rb:13:in `call'
rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service'
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service'
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run'
/home/pratik/.rvm/rubies/ruby-1.9.2-p0/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread'
I had a similar problem, but i was able to solve this with the following code:
module Connection
class DB1 < ActiveRecord::Base
self.abstract_class = true
establish_connection(Rails.env)
end
class DB2 < ActiveRecord::Base
self.abstract_class = true
establish_connection("#{Rails.env}_db2")
end
end
class Project < Connection::DB1
Maybe it worked for me because of the usages of establish_connection in all models.
I don't have time to test this so I cannot say for sure, but this line looks suspect to me:
self.calendars.create(:name => "default")
I would think it should be like this:
Calendar.create(:name => "default", :project_id => self.id)