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
Related
I am trying to CRUD my rails app and I am stuck. The form displays at /pins/new but after hitting submit I run into this ERROR.
Let me know if more files are required to solve this. It is saying that there is an undefined method 'resource_type' but I don't have a method by that name and is where I am confused. Been stuck on this for a couple days. Have googled a bunch and tried out various solutions with no luck. Please help. I appreciate you.
Getting ERROR
undefined method `resource_type' for # Did you mean? restore_text!
Github can be found at https://github.com/dustin-longenecker/rails-pinning-app
The last commit is before adding the get / post routes for creating a new pin.
Pointed at pins_controller.rb LINE 21 in the #CREATE method
class PinsController < ApplicationController
def index
#pins = Pin.all
end
def show
#pin = Pin.find(params[:id])
end
def show_by_name
#pin = Pin.find_by_slug(params[:slug])
render :show
end
def new
#pin = Pin.new
end
def create
#pin = Pin.create(pin_params)
redirect :show
end
private
def pin_params
params.require(:pin).permit(:title, :url, :slug, :text, :category_id)
end
end
routes.rb
root 'pins#index'
#get "pins/name-:slug" => "pins#show_by_name"
get "pins/name-:slug" => "pins#show_by_name", as: 'pin_by_name'
resources :pins
get "library" => "pins#index"
get "/pins/new" => "pins#new"
post "/pins" => "pins#create"
new.html.erb
<%= form_for #pin do |f| %>
<div class="form-group">
<label for="pin_title">Title</label>
<%= f.text_field(:title, class: "form-control") %>
</div>
<div class="form-group">
<label for="pin_url">URL</label>
<%= f.url_field(:url, class: "form-control") %>
</div>
<div class="form-group">
<label for="pin_text">Description</label>
<%= f.text_area(:text, class: "form-control") %>
</div>
<div class="form-group">
<label for="pin_category_id">Category</label>
<%= f.collection_select(:category_id, Category.all, :id, :name, class: "form-control") %>
</div>
<div class="form-group">
<%= f.submit(class: "btn btn-default")%>
</div>
<% end %>
MODEL pin.rb
class Pin < ActiveRecord::Base
validates_presence_of :title, :url, :slug, :text, :resource_type
validates_uniqueness_of :slug
belongs_to :category
end
MODEL category.rb
class Category < ActiveRecord::Base
has_many :pins
end
Migration create_category.rb
class CreateCategories < ActiveRecord::Migration[4.2]
def change
create_table :categories do |t|
t.string :name
end
add_column :pins, :category_id, :integer, references: :categories
add_index :pins, :category_id
if Category.all.empty?
Category.create(name: "ruby")
Category.create(name: "rails")
Category.create(name: "unknown")
end
unknown_category = Category.find_by_name("unknown")
pins = Pin.where("category_id is null")
pins.each do |pin|
category = Category.find_by_name(pin.resource_type)
if category.present?
pin.category_id = category.id
else
pin.category_id = unknown_category.id
end
pin.save
end
if Pin.where("category_id is null").empty?
remove_column :pins, :resource_type
puts "MIGRATION SUCCESSFUL!"
puts "All your data has been migrated successfully."
else
puts "ERROR! Something went wrong - not all pins have been assigned a category Id."
end
end
end
ERROR STACK
activemodel (5.2.2) lib/active_model/attribute_methods.rb:430:in `method_missing'
activemodel (5.2.2) lib/active_model/validator.rb:150:in `block in validate'
activemodel (5.2.2) lib/active_model/validator.rb:149:in `each'
activemodel (5.2.2) lib/active_model/validator.rb:149:in `validate'
activesupport (5.2.2) lib/active_support/callbacks.rb:426:in `block in make_lambda'
activesupport (5.2.2) lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'
activesupport (5.2.2) lib/active_support/callbacks.rb:606:in `block (2 levels) in default_terminator'
activesupport (5.2.2) lib/active_support/callbacks.rb:605:in `catch'
activesupport (5.2.2) lib/active_support/callbacks.rb:605:in `block in default_terminator'
activesupport (5.2.2) lib/active_support/callbacks.rb:199:in `block in halting'
activesupport (5.2.2) lib/active_support/callbacks.rb:513:in `block in invoke_before'
activesupport (5.2.2) lib/active_support/callbacks.rb:513:in `each'
activesupport (5.2.2) lib/active_support/callbacks.rb:513:in `invoke_before'
activesupport (5.2.2) lib/active_support/callbacks.rb:131:in `run_callbacks'
activesupport (5.2.2) lib/active_support/callbacks.rb:816:in `_run_validate_callbacks'
activemodel (5.2.2) lib/active_model/validations.rb:409:in `run_validations!'
activemodel (5.2.2) lib/active_model/validations/callbacks.rb:118:in `block in run_validations!'
activesupport (5.2.2) lib/active_support/callbacks.rb:98:in `run_callbacks'
activesupport (5.2.2) lib/active_support/callbacks.rb:816:in `_run_validation_callbacks'
activemodel (5.2.2) lib/active_model/validations/callbacks.rb:118:in `run_validations!'
activemodel (5.2.2) lib/active_model/validations.rb:339:in `valid?'
activerecord (5.2.2) lib/active_record/validations.rb:67:in `valid?'
activerecord (5.2.2) lib/active_record/validations.rb:84:in `perform_validations'
activerecord (5.2.2) lib/active_record/validations.rb:46:in `save'
activerecord (5.2.2) lib/active_record/transactions.rb:310:in `block (2 levels) in save'
activerecord (5.2.2) lib/active_record/transactions.rb:387:in `block in with_transaction_returning_status'
activerecord (5.2.2) lib/active_record/connection_adapters/abstract/database_statements.rb:259:in `block in transaction'
activerecord (5.2.2) lib/active_record/connection_adapters/abstract/transaction.rb:239:in `block in within_new_transaction'
/Users/dl/.rvm/rubies/ruby-2.6.3/lib/ruby/2.6.0/monitor.rb:230:in `mon_synchronize'
activerecord (5.2.2) lib/active_record/connection_adapters/abstract/transaction.rb:236:in `within_new_transaction'
activerecord (5.2.2) lib/active_record/connection_adapters/abstract/database_statements.rb:259:in `transaction'
activerecord (5.2.2) lib/active_record/transactions.rb:212:in `transaction'
activerecord (5.2.2) lib/active_record/transactions.rb:385:in `with_transaction_returning_status'
activerecord (5.2.2) lib/active_record/transactions.rb:310:in `block in save'
activerecord (5.2.2) lib/active_record/transactions.rb:325:in `rollback_active_record_state!'
activerecord (5.2.2) lib/active_record/transactions.rb:309:in `save'
activerecord (5.2.2) lib/active_record/suppressor.rb:44:in `save'
activerecord (5.2.2) lib/active_record/persistence.rb:36:in `create'
app/controllers/pins_controller.rb:21:in `create'
actionpack (5.2.2) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (5.2.2) lib/abstract_controller/base.rb:194:in `process_action'
actionpack (5.2.2) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.2.2) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (5.2.2) lib/active_support/callbacks.rb:132:in `run_callbacks'
actionpack (5.2.2) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (5.2.2) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (5.2.2) lib/action_controller/metal/instrumentation.rb:34:in `block in process_action'
activesupport (5.2.2) lib/active_support/notifications.rb:168:in `block in instrument'
activesupport (5.2.2) lib/active_support/notifications/instrumenter.rb:23:in `instrument'
activesupport (5.2.2) lib/active_support/notifications.rb:168:in `instrument'
actionpack (5.2.2) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (5.2.2) lib/action_controller/metal/params_wrapper.rb:256:in `process_action'
activerecord (5.2.2) lib/active_record/railties/controller_runtime.rb:24:in `process_action'
actionpack (5.2.2) lib/abstract_controller/base.rb:134:in `process'
actionview (5.2.2) lib/action_view/rendering.rb:32:in `process'
actionpack (5.2.2) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (5.2.2) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (5.2.2) lib/action_dispatch/routing/route_set.rb:52:in `dispatch'
actionpack (5.2.2) lib/action_dispatch/routing/route_set.rb:34:in `serve'
actionpack (5.2.2) lib/action_dispatch/journey/router.rb:52:in `block in serve'
actionpack (5.2.2) lib/action_dispatch/journey/router.rb:35:in `each'
actionpack (5.2.2) lib/action_dispatch/journey/router.rb:35:in `serve'
actionpack (5.2.2) lib/action_dispatch/routing/route_set.rb:840:in `call'
rack (2.2.2) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.2.2) lib/rack/etag.rb:27:in `call'
rack (2.2.2) lib/rack/conditional_get.rb:40:in `call'
rack (2.2.2) lib/rack/head.rb:12:in `call'
actionpack (5.2.2) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.2.2) lib/rack/session/abstract/id.rb:266:in `context'
rack (2.2.2) lib/rack/session/abstract/id.rb:260:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/cookies.rb:670:in `call'
activerecord (5.2.2) lib/active_record/migration.rb:559:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
activesupport (5.2.2) lib/active_support/callbacks.rb:98:in `run_callbacks'
actionpack (5.2.2) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/debug_exceptions.rb:61:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.2) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.2) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.2) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.2) lib/rack/method_override.rb:24:in `call'
rack (2.2.2) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.2) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.2) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.2.2) lib/rack/sendfile.rb:110:in `call'
railties (5.2.2) lib/rails/engine.rb:524:in `call'
puma (4.3.3) lib/puma/configuration.rb:228:in `call'
puma (4.3.3) lib/puma/server.rb:682:in `handle_request'
puma (4.3.3) lib/puma/server.rb:472:in `process_client'
puma (4.3.3) lib/puma/server.rb:328:in `block in run'
puma (4.3.3) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
You have a validation that references resource_type:
class Pin < ActiveRecord::Base
validates_presence_of :title, :url, :slug, :text, :resource_type # its right here!
validates_uniqueness_of :slug
belongs_to :category
end
If you actually meant to have it then you need to add the column to the database with a migration.
And that's not how you create a resource.
class PinsController < ApplicationController
# ...
# POST /pins
def create
#pin = Pin.new(pin_params)
if #pin.save
redirect_to #pin
else
render :new
end
end
# ...
end
I've been trying to learn ruby(2.6.3) on rails(6.0.1) by following a project to make an Instagram clone.
When I try to create a new post I get "no implicit conversion of String into Integer app/controllers/posts_controller.rb:8:in `create' ". I'm confused because I do not see a string to int so i'm wondering if it is something i'm missing with ruby. I've tried updating my gem versions and also tried adding the "strong_parameters" gem but that did not seem to fix my problem. Thank you for any insight as to what i'm doing wrong
Post_controller.rb
class PostsController < ApplicationController
def new
#post = Post.new
end
def create
#post = Post.new(post_params)
if #post.save
redirect_to dashboard_path, flash: {success: "Post was created successfully!"}
else
redirect_to new_post_path, flash: {danger: "Post failed to save!"}
end
end
def show
end
private
def post_params
params.require(:post).permit(:image, :image_cache)
end
end
Posts table migrate
class CreatePosts < ActiveRecord::Migration[6.0]
def change
create_table :posts do |t|
t.string :image
t.boolean :active
t.references :account
t.timestamps
end
end
end
Post Model
class Post < ApplicationRecord
mount_uploader :image, ImageUploader
end
Post table schema
create_table "posts", force: :cascade do |t|
t.string "image"
t.boolean "active"
t.bigint "account_id"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["account_id"], name: "index_posts_on_account_id"
end
Full stack trace
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:312:in `chmod'
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:312:in `chmod!'
carrierwave (2.0.2) lib/carrierwave/sanitized_file.rb:220:in `copy_to'
carrierwave (2.0.2) lib/carrierwave/uploader/cache.rb:141:in `cache!'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:63:in `block (2 levels) in cache'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:176:in `handle_error'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:47:in `block in cache'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:46:in `map'
carrierwave (2.0.2) lib/carrierwave/mounter.rb:46:in `cache'
carrierwave (2.0.2) lib/carrierwave/mount.rb:146:in `image='
carrierwave (2.0.2) lib/carrierwave/mount.rb:373:in `image='
carrierwave (2.0.2) lib/carrierwave/orm/activerecord.rb:75:in `image='
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:51:in `public_send'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:51:in `_assign_attribute'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:44:in `block in _assign_attributes'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:43:in `each'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:43:in `_assign_attributes'
activerecord (6.0.1) lib/active_record/attribute_assignment.rb:22:in `_assign_attributes'
activemodel (6.0.1) lib/active_model/attribute_assignment.rb:35:in `assign_attributes'
activerecord (6.0.1) lib/active_record/core.rb:326:in `initialize'
activerecord (6.0.1) lib/active_record/inheritance.rb:70:in `new'
activerecord (6.0.1) lib/active_record/inheritance.rb:70:in `new'
app/controllers/posts_controller.rb:8:in `create'
actionpack (6.0.1) lib/action_controller/metal/basic_implicit_render.rb:6:in `send_action'
actionpack (6.0.1) lib/abstract_controller/base.rb:196:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (6.0.1) lib/abstract_controller/callbacks.rb:42:in `block in process_action'
activesupport (6.0.1) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (6.0.1) lib/abstract_controller/callbacks.rb:41:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/rescue.rb:22:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/instrumentation.rb:33:in `block in process_action'
activesupport (6.0.1) lib/active_support/notifications.rb:180:in `block in instrument'
activesupport (6.0.1) lib/active_support/notifications/instrumenter.rb:24:in `instrument'
activesupport (6.0.1) lib/active_support/notifications.rb:180:in `instrument'
actionpack (6.0.1) lib/action_controller/metal/instrumentation.rb:32:in `process_action'
actionpack (6.0.1) lib/action_controller/metal/params_wrapper.rb:245:in `process_action'
activerecord (6.0.1) lib/active_record/railties/controller_runtime.rb:27:in `process_action'
actionpack (6.0.1) lib/abstract_controller/base.rb:136:in `process'
actionview (6.0.1) lib/action_view/rendering.rb:39:in `process'
actionpack (6.0.1) lib/action_controller/metal.rb:191:in `dispatch'
actionpack (6.0.1) lib/action_controller/metal.rb:252:in `dispatch'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:51:in `dispatch'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:33:in `serve'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:49:in `block in serve'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:32:in `each'
actionpack (6.0.1) lib/action_dispatch/journey/router.rb:32:in `serve'
actionpack (6.0.1) lib/action_dispatch/routing/route_set.rb:837:in `call'
warden (1.2.8) lib/warden/manager.rb:36:in `block in call'
warden (1.2.8) lib/warden/manager.rb:34:in `catch'
warden (1.2.8) lib/warden/manager.rb:34:in `call'
rack (2.0.7) lib/rack/tempfile_reaper.rb:15:in `call'
rack (2.0.7) lib/rack/etag.rb:25:in `call'
rack (2.0.7) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.7) lib/rack/head.rb:12:in `call'
actionpack (6.0.1) lib/action_dispatch/http/content_security_policy.rb:18:in `call'
rack (2.0.7) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.7) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/cookies.rb:648:in `call'
activerecord (6.0.1) lib/active_record/migration.rb:567:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/callbacks.rb:27:in `block in call'
activesupport (6.0.1) lib/active_support/callbacks.rb:101:in `run_callbacks'
actionpack (6.0.1) lib/action_dispatch/middleware/callbacks.rb:26:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/actionable_exceptions.rb:17:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/debug_exceptions.rb:32:in `call'
web-console (4.0.1) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.0.1) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `catch'
web-console (4.0.1) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (6.0.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.1) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/method_override.rb:22:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
actionpack (6.0.1) lib/action_dispatch/middleware/host_authorization.rb:83:in `call'
webpacker (4.2.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.6.5) lib/rack/proxy.rb:57:in `call'
railties (6.0.1) lib/rails/engine.rb:526:in `call'
puma (4.3.1) lib/puma/configuration.rb:228:in `call'
puma (4.3.1) lib/puma/server.rb:681:in `handle_request'
puma (4.3.1) lib/puma/server.rb:472:in `process_client'
puma (4.3.1) lib/puma/server.rb:328:in `block in run'
puma (4.3.1) lib/puma/thread_pool.rb:134:in `block in spawn_thread'
Carrierwave config
CarrierWave.configure do |config| config.fog_credentials = { provider: 'AWS', aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'], aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], } config.storage = :fog config.permissions = "0666" config.cache_dir = "#{Rails.root}/tmp/" config.fog_directory = ENV['FOG_DIRECTORY'] config.fog_attributes = { 'Cache-Control' => "max-age=#{365.day.to_i}" }end
almost always when I see this, it is passing a string into the index of an array. Sorry to be vague, but I don't see where that would happen in your code.
I'm using Rails 5.1.6 with Puma and MySQL.
I have the below code in a Rails controller:
class ScansController < ApplicationController
before_action :find_device, only: %i[index new create]
.
.
.
def create
#scan = #device.scans.create(scan_params)
flash[:notice] = t('.success') if #scan.save
respond_with #scan.device, #scan
end
.
.
.
private
def find_device
#device = Device.find params[:device_id]
end
def scan_params
params.require(:scan).permit(:photo, :device_id, :reading, :reading_date)
end
end
And when I send a POST request to this URL the below happens:
Started POST "/en/devices/1/scans" for 81.0.89.20 at 2018-04-03 09:39:19 +0000
09:39:19 web.1 | Processing by ScansController#create as HTML
09:39:19 web.1 | Parameters: {"utf8"=>"✓", "authenticity_token"=>"QFdUs2thGPzJMbcWfApbattFMvnIEfzN1swrVGnbOTx7XbjY0Y3A8PLI1bRxqyBv6xIk9D2t13bfLPAqdSIyzQ==", "scan"=>{"reading_date"=>"2018-04-03T11:39:11", "reading"=>"6"}, "button"=>"", "locale"=>"en", "device_id"=>"1"}
09:39:19 web.1 | User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
09:39:19 web.1 | Device Load (0.6ms) SELECT `devices`.* FROM `devices` WHERE `devices`.`id` = 1 LIMIT 1
09:39:19 web.1 | (0.6ms) BEGIN
09:39:19 web.1 | (0.7ms) ROLLBACK
09:39:19 web.1 | Completed 500 Internal Server Error in 33ms (ActiveRecord: 2.9ms)
09:39:19 web.1 |
09:39:19 web.1 |
09:39:19 web.1 |
09:39:19 web.1 | SystemStackError (stack level too deep):
09:39:19 web.1 |
09:39:19 web.1 | app/controllers/scans_controller.rb:35:in `create'
Now, line 35 is this line:#scan = #device.scans.create(scan_params)
and this was working well in Rails 4, but now that I migrated to Rails 5.1.6 it produces this behaviior even in my integration tests.
I hav no clue what could be the reason for this behavior. Does anyone sees something my eyes avoided for the last one or two weeks?
Thanx in advance.
The models:
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Scan < ApplicationRecord
.
.
belongs_to :device, inverse_of: :scans
validates_attachment(
:photo,
content_type: {
content_type: [
'image/jpeg',
'image/gif',
'image/png',
'image/tiff'
]
}
)
with_options unless: #photo.present? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
.
end
class Device < ApplicationRecord
.
.
has_many :scans, inverse_of: :device, dependent: :restrict_with_error
validates :unit, presence: true
validates :name, presence: true
validates :serial, presence: true, uniqueness: true
validates :owner, presence: true
validates :precision, numericality: {
only_integer: true,
greater_than_or_equal_to: 0
}, allow_nil: true
.
.
end
The Full stack trace looks like this:
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
activesupport (5.1.6) lib/active_support/callbacks.rb:422:in `block in inverted_lambda'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block (2 levels) in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `each'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `all?'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in `block in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `block in invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `each'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in `invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:130:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in `run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in `_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in `run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in `valid?'
activerecord (5.1.6) lib/active_record/validations.rb:82:in `perform_validations'
activerecord (5.1.6) lib/active_record/validations.rb:44:in `save'
activerecord (5.1.6) lib/active_record/attribute_methods/dirty.rb:35:in `save'
activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block (2 levels) in save'
activerecord (5.1.6) lib/active_record/transactions.rb:384:in `block in with_transaction_returning_status'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:233:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:381:in `with_transaction_returning_status'
activerecord (5.1.6) lib/active_record/transactions.rb:308:in `block in save'
activerecord (5.1.6) lib/active_record/transactions.rb:323:in `rollback_active_record_state!'
activerecord (5.1.6) lib/active_record/transactions.rb:307:in `save'
activerecord (5.1.6) lib/active_record/suppressor.rb:42:in `save'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:16:in `block in save'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/transaction.rb:917:in `with_database_metric_name'
newrelic_rpm (5.0.0.342) lib/new_relic/agent.rb:618:in `with_database_metric_name'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/active_record_prepend.rb:15:in `save'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:371:in `insert_record'
activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:34:in `insert_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:360:in `block (2 levels) in _create_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:447:in `replace_on_target'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:281:in `add_to_target'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:358:in `block in _create_record'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:129:in `block in transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `block in transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:194:in `block in within_new_transaction'
/usr/local/rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/monitor.rb:226:in `mon_synchronize'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/transaction.rb:191:in `within_new_transaction'
activerecord (5.1.6) lib/active_record/connection_adapters/abstract/database_statements.rb:235:in `transaction'
activerecord (5.1.6) lib/active_record/transactions.rb:210:in `transaction'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:128:in `transaction'
activerecord (5.1.6) lib/active_record/associations/collection_association.rb:357:in `_create_record'
activerecord (5.1.6) lib/active_record/associations/has_many_association.rb:121:in `_create_record'
activerecord (5.1.6) lib/active_record/associations/association.rb:196:in `create'
activerecord (5.1.6) lib/active_record/associations/collection_proxy.rb:347:in `create'
actionpack (5.1.6) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.6) lib/active_support/callbacks.rb:108:in `block in run_callbacks'
activesupport (5.1.6) lib/active_support/core_ext/time/zones.rb:64:in `use_zone'
browser-timezone-rails (1.0.1) lib/browser-timezone-rails.rb:15:in `set_time_zone'
activesupport (5.1.6) lib/active_support/callbacks.rb:117:in `block in run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:135:in `run_callbacks'
actionpack (5.1.6) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.6) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.6) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.6) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.6) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.6) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6) lib/action_dispatch/routing/route_set.rb:844:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
warden (1.2.7) lib/warden/manager.rb:36:in `block in call'
warden (1.2.7) lib/warden/manager.rb:35:in `catch'
warden (1.2.7) lib/warden/manager.rb:35:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/etag.rb:25:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/conditional_get.rb:38:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/head.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92: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'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/cookies.rb:613:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activerecord (5.1.6) lib/active_record/migration.rb:556:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:24:in `block in call'
rollbar (2.15.5) lib/rollbar.rb:145:in `scoped'
rollbar (2.15.5) lib/rollbar/middleware/rails/rollbar.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
rollbar (2.15.5) lib/rollbar/middleware/rails/show_exceptions.rb:22:in `call_with_rollbar'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.1.6) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6) lib/rails/rack/logger.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
request_store (1.4.1) lib/request_store/middleware.rb:19:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/request_id.rb:25:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/method_override.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/runtime.rb:22:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
activesupport (5.1.6) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack-rewrite (1.5.1) lib/rack/rewrite.rb:24:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/executor.rb:12:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
actionpack (5.1.6) lib/action_dispatch/middleware/static.rb:125:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
rack (2.0.4) lib/rack/sendfile.rb:111:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92:in `call'
railties (5.1.6) lib/rails/engine.rb:522:in `call'
newrelic_rpm (5.0.0.342) lib/new_relic/agent/instrumentation/middleware_tracing.rb:92: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'
This is the part which is repeating in the stack trace:
activesupport (5.1.6) lib/active_support/callbacks.rb:422:in 'block in inverted_lambda'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block (2 levels) in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'each'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'all?'
activesupport (5.1.6) lib/active_support/callbacks.rb:177:in 'block in halting_and_conditional'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'block in invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'each'
activesupport (5.1.6) lib/active_support/callbacks.rb:507:in 'invoke_before'
activesupport (5.1.6) lib/active_support/callbacks.rb:130:in 'run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validate_callbacks'
activemodel (5.1.6) lib/active_model/validations.rb:405:in 'run_validations!'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'block in run_validations!'
activesupport (5.1.6) lib/active_support/callbacks.rb:131:in 'run_callbacks'
activesupport (5.1.6) lib/active_support/callbacks.rb:827:in '_run_validation_callbacks'
activemodel (5.1.6) lib/active_model/validations/callbacks.rb:114:in 'run_validations!'
activemodel (5.1.6) lib/active_model/validations.rb:335:in 'valid?'
activerecord (5.1.6) lib/active_record/validations.rb:65:in 'valid?'
It's a race condition.
I think you can only have 1 inverse_of since scan will call device and vice versa.
I've got the solution, although I don't exactly understand why it was working till now.
Originally there was the below validation in the Scan model (as I posted earlier):
with_options unless: #photo.present? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
In irb I managed to localize the error to this validation part. After refactoring the #photo.present? to a new method and calling that method in the validation, the problem is solved:
with_options unless: :has_photo? do
validates :reading, presence: true
validates :reading, numericality: true
validates :reading_date, presence: true
end
def has_photo?
#photo.present?
end
According to the document:
:unless option:
Specifies a method, proc or string to call to determine if
the validation should not occur. The method, proc
or string should return or evaluate to a true or false value.
But, your model Scan have unless: #photo.present?:
with_options unless: #photo.present? do
validates :reading, presence: true
...
end
That will lead to unexpected exceptions. In order to solve this issue, we should use any one of them below:
# Option 1:
with_options unless: proc { |scan| scan.photo.present? }
# Option 2:
with_options unless: 'photo.present?'
# Option 3:
with_options unless: :photo_present?
private
def photo_present?
photo.present?
end
In my case I encountered a similar error due to bad naming, please consider checking if the names could interfere with something inside of rails
I get NameError: uninitialized constant Category::Translation when executing:
Category.first.translations
Rails version: 5.1.3
Ruby version: 2.3.3
Models:
# app/models/translation.rb
class Translation < ApplicationRecord
belongs_to :translatable, polymorphic: true
end
# app/models/category.rb
class Category < ApplicationRecord
has_many :translations, :as => :translatable
end
Migration:
class CreateTranslations < ActiveRecord::Migration[5.1]
def change
create_table :translations do |t|
t.string :locale
t.string :key
t.text :value
t.text :interpolations
t.boolean :is_proc, default: false
t.references :translatable, polymorphic: true, index: true
t.timestamps
end
end
end
schema.rb:
create_table "translations", force: :cascade do |t|
t.string "locale"
t.string "key"
t.text "value"
t.text "interpolations"
t.boolean "is_proc", default: false
t.string "translatable_type"
t.integer "translatable_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["translatable_type", "translatable_id"], name: "index_translations_on_translatable_type_and_translatable_id"
end
Full trace:
activerecord (5.1.3) lib/active_record/inheritance.rb:166:in `compute_type'
activerecord (5.1.3) lib/active_record/reflection.rb:401:in `compute_class'
activerecord (5.1.3) lib/active_record/reflection.rb:397:in `klass'
activerecord (5.1.3) lib/active_record/associations/association.rb:127:in `klass'
activerecord (5.1.3) lib/active_record/associations/collection_association.rb:33:in `reader'
activerecord (5.1.3) lib/active_record/associations/builder/association.rb:111:in `translations'
app/controllers/categories_controller.rb:27:in `new'
actionpack (5.1.3) lib/action_controller/metal/basic_implicit_render.rb:4:in `send_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:186:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rendering.rb:30:in `process_action'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (5.1.3) lib/active_support/callbacks.rb:131:in `run_callbacks'
actionpack (5.1.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/rescue.rb:20:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `block in instrument'
activesupport (5.1.3) lib/active_support/notifications/instrumenter.rb:21:in `instrument'
activesupport (5.1.3) lib/active_support/notifications.rb:166:in `instrument'
actionpack (5.1.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (5.1.3) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.3) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.3) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.3) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.3) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.3) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:46:in `block in serve'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.3) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.3) lib/action_dispatch/routing/route_set.rb:834:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/strategy.rb:189:in `call!'
omniauth (1.6.1) lib/omniauth/strategy.rb:167:in `call'
omniauth (1.6.1) lib/omniauth/builder.rb:63:in `call'
rack (2.0.3) lib/rack/etag.rb:25:in `call'
rack (2.0.3) lib/rack/conditional_get.rb:25:in `call'
rack (2.0.3) lib/rack/head.rb:12:in `call'
rack (2.0.3) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.3) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/cookies.rb:613:in `call'
activerecord (5.1.3) lib/active_record/migration.rb:556:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.3) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.3) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) 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.3) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.3) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.3) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.3) lib/rails/rack/logger.rb:24:in `call'
sprockets-rails (3.2.0) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.3) lib/rack/method_override.rb:22:in `call'
rack (2.0.3) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.3) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.3) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.3) lib/rack/sendfile.rb:111:in `call'
railties (5.1.3) lib/rails/engine.rb:522:in `call'
puma (3.9.1) lib/puma/configuration.rb:224:in `call'
puma (3.9.1) lib/puma/server.rb:602:in `handle_request'
puma (3.9.1) lib/puma/server.rb:435:in `process_client'
puma (3.9.1) lib/puma/server.rb:299:in `block in run'
puma (3.9.1) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
I read on many sites that this error is caused by misconfiguration in assocations (mostly typos). I also tried to set :classname => 'Translation' in Category class, but that didnt helped too. I was not able to identify the problem for the whole day so I am here to beg for help.
Thanks!
I solved it! This was not related to the models directly. #MrYoshiji pointed me to the right direction.
I use I18n gem and i18n-active_record so I can store translations in database.
In initialization Rails loaded this code:
Translation = I18n::Backend::ActiveRecord::Translation
The Translation model was in real I18n::Backend::ActiveRecord::Translation model. And therefore changes in my Translation model took no effect, because the name was linked to another model..
I changed app/models/translation.rb to this:
class Translation < I18n::Backend::ActiveRecord::Translation
belongs_to :translatable, polymorphic: true
end
and deleted line mentioned above from configuration.
Anyway, thank you so much for your comments!
I'm trying to get a number of users to be registered in a team , I updated my code so that a team can get registered, however the following error shows up :
undefined method `each' for "27":String
NoMethodError in TeamsController#create
My code is as follows:
class Team<ActiveRecord::Base
belongs_to :league
belongs_to :seed
has_many :speakers do
def user(level="1")
find_by(level: level).user
end
end
end
my user model looks like this :
class User < ActiveRecord::Base
belongs_to :team
end
user model:
class User<ActiveRecord::Base
has_many :speaking_engagements, class_name: "Speaker"
has_many :teams , through: :speaking_engagements
end
speaker model:
class Speaker < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
Team Controller:
class TeamsController<ApplicationController
def new
#seed=Seed.find_by_id(params[:seed_id])
#league=current_admin.league
#team=current_admin.league.teams.build(:seed_id=>#seed,:approved=>false)
#usernames= #mca.connections.connected.each do |x| x.user end
end
def create
#league=current_admin.league
**#team = #league.teams.build(team_params)** #problem appears to be here
if #team.save
flash[:notice] = "Team Request Sent!."
redirect_to '/'
else
flash[:error] = "Unable to request team."
redirect_to :back
end
end
form looks like:
<div class="panel-body">
<div class="container">
<%= form_for #team do |f| %>
<%= f.hidden_field :seed_id, :value => #seed.id %>
<%= f.hidden_field :league_id, :value => #league.id %>
<div class="row">
<!-- <div class="col-md-8"> -->
<div class="form-group">
<%= f.collection_select :speakers, #usernames,:user_id,:fullname, multiple:true %>
</div>
<!-- </div> -->
</div>
<div class="actions">
<%= f.submit "Create" , class:"btn btn-primary" %>
</div>
<% end %>
</div>
</div>
Speaker migration:
class CreateSpeekers < ActiveRecord::Migration
def change
create_table :speakers do |t|
t.integer :team_id
t.integer :user_id
t.integer :level
t.timestamps null: false
end
end
end
I've been stuck with this error for quite sometime now, I would much appreciate any help!
Full trace:
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:355:in `replace'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:47:in `writer'
activerecord (4.2.0) lib/active_record/associations/builder/association.rb:123:in `speekers='
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `public_send'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:41:in `block in assign_attributes'
actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
actionpack (4.2.0) lib/action_controller/metal/strong_parameters.rb:183:in `each_pair'
activerecord (4.2.0) lib/active_record/attribute_assignment.rb:35:in `assign_attributes'
activerecord (4.2.0) lib/active_record/core.rb:557:in `init_attributes'
activerecord (4.2.0) lib/active_record/core.rb:280:in `initialize'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/inheritance.rb:61:in `new'
activerecord (4.2.0) lib/active_record/reflection.rb:131:in `build_association'
activerecord (4.2.0) lib/active_record/associations/association.rb:247:in `build_record'
activerecord (4.2.0) lib/active_record/associations/collection_association.rb:136:in `build'
activerecord (4.2.0) lib/active_record/associations/collection_proxy.rb:254:in `build'
app/controllers/teams_controller.rb:13:in `create'
actionpack (4.2.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:234:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:169:in `block in halting'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:92:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_process_action_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:31:in `block in process_action'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.0) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.0) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.0) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.0) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.0) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.0) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.0) lib/action_controller/metal.rb:195:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.0) lib/action_controller/metal.rb:236:in `block in action'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:42:in `serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.0) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.0) lib/action_dispatch/routing/route_set.rb:802:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:38:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.0) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:647:in `call'
activerecord (4.2.0) lib/active_record/migration.rb:378:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `call'
activesupport (4.2.0) lib/active_support/callbacks.rb:88:in `_run_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:734:in `_run_call_callbacks'
activesupport (4.2.0) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.0) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.0) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.0) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.0) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.0) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.0) lib/action_dispatch/middleware/static.rb:113:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.0) lib/rails/engine.rb:518:in `call'
railties (4.2.0) lib/rails/application.rb:164:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
/home/shyam/.rbenv/versions/2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
The problem is that "27" (a user id) is being assigned as the speakers association for a team. This is wrong. speakers should be assigned instances of Speaker.
Unfortunately it gets tough at this point.
What you really need to do is use nested attributes to build the desired speaker records, where each speaker is assign one of the selected user ids. There are many ways to approach this in the UI. There are many great examples of using nested attributes, including this Railscasts episode
has_many gives you a lot of interesting methods, including collection_singular_ids and collection_singular_ids=. The gist of which means you should be able to leverage that getter & setter in your form like this
<%= f.collection_select :speaker_ids, #usernames, :user_id, :fullname, multiple: true %>
i found the answer to my question, thanks to following #WizardOfOgz comment,
so i did the following:
in my teams_controller.rb added the following parameters as permitted :
params.require(:team).permit(:user_id, :league_id,:seed_id, :approved, :speakers_attributes=>[:team_id,:user_id])
end
then,
in my team.rb added the follwing :
accepts_nested_attributes_for :speakers
further, added nested attributes in the form for team:
(in app/views/teams/_form.html.rb )
<h2>Speakers</h2>
<%= f.fields_for :speakers do |ff| %>
<div>
<%= ff.collection_select :user_id, #usernames,:user_id,:fullname%>
</div>
<% end %>
also since I wanted 3 speakers, I created the following function in helpers/form_helpers:
module FormHelper
def setup_team(team)
3.times{
team.speakers.build
}
team
end
end
that solved it for me,
thanks to #WizardOfOgz and #RichPeck
also this one really really helped:
http://www.sitepoint.com/complex-rails-forms-with-nested-attributes/