I'm having a little difficulty groking the call back model and methodology in Factory Girl. Using this post http://robots.thoughtbot.com/post/254496652/aint-no-calla-back-girl I started giving it a go and, now I'm erroring in the spec. I am new to Rails...so I may be missing something really fundamental.
The Error is:
Failure/Error: #user = Factory(:admin_user)
undefined method 'each' for #
The Models:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :lockable,
:recoverable, :rememberable, :trackable, :validatable, :token_authenticatble
# Setup accessible (or protected) attributes for your model
attr_accessible :username, :email, :role_ids, :password, :password_confirmation, :remember_me
has_many :userroles
has_many :roles, :through=>:userroles
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
end
class Role < ActiveRecord::Base
attr_accessible :name
validates :name, :presence=>true,
:length=>{:minimum=>4, :maximum=>30}
has_many :userroles
has_many :users, :through=>:userroles
end
class Userrole < ActiveRecord::Base
attr_accessible :user_id, :role_id
belongs_to :user
belongs_to :role
end
The Factories:
Factory.define :user do |f|
f.email 'someone#somecompany.com'
f.username 'foo'
f.password '123456'
end
Factory.define :role do |r|
r.name 'testrole'
end
Factory.define :userrole do |ur|
ur.association(:role)
ur.userrole{|ur| ur.userrole(:user)}
end
Factory.define :admin_role, :class => 'Role' do |userrole|
userrole.role { |role| role.association(:role, :name => "Admin") }
end
Factory.define :admin_user, :parent => :user do |user|
user.after_create { |u| Factory(:admin_role, :userroles => u) }
end
Lastly the simple spec:
require File.dirname(__FILE__) + '/../spec_helper'
describe RolesController do
fixtures :all
include Devise::TestHelpers
render_views
before (:each) do
#user = Factory(:admin_user)
sign_in #user
end
it "index action should render index template" do
get :index
response.should render_template(:index)
end
end
Update: Adding Stack Trace
ruby-1.9.2-p136 :001 > u=Factory(:admin_user)
NoMethodError: undefined method `each' for #<User:0x00000004d737d8>
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/activerecord-3.0.3/lib/active_record/associations/association_collection.rb:352:in `replace'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/activerecord-3.0.3/lib/active_record/associations.rb:1524:in `block in collection_accessor_methods'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/build.rb:13:in `set'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `block in run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `each'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:320:in `run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
from /home/ryan/Sites/Holocron/spec/factories/users.rb:22:in `block (2 levels) in <top (required)>'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `call'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:29:in `block in run_callbacks'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `each'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy.rb:28:in `run_callbacks'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/proxy/create.rb:7:in `result'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:327:in `run'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:270:in `create'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl/factory.rb:301:in `default_strategy'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/factory_girl-1.3.2/lib/factory_girl.rb:20:in `Factory'
from (irb):1
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
from /home/ryan/.rvm/gems/ruby-1.9.2-p136#Holocron/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>'
I believe you've confused yourself.
You've defined Factory.define :admin_role, :class => 'Role' do |userrole| as a Role but then in your factory definition you treat it like a Userrole. (BTW, Ruby conventions use underlines – so Userrole would be UserRole/user_role.)
Also, your user.after_create { |u| Factory(:admin_role, :userroles => u) } probably ought to be: user.after_create { |u| Factory(:admin_role, :userroles => [u]) } since a Role has_many :userroles.
Related
I receive this message when i attempt to create a new record from a nested form. The strange thing is when I just backspace to the previous page(page with the 'create' button) and hit 'create' again, the record is created. So I am not sure why it won't create the record the first time as I don't change the create method in my controller during the first and second button press. Anyone experience this before that can help me understand why this is happening would be great.
Models
class Benefit < ActiveRecord::Base
belongs_to :account
has_many :employee_benefits
has_many :benefit_plans, :inverse_of => :benefit
belongs_to :benefit_coverage_period
belongs_to :benefit_type_id, :class_name => "LookupTable", :foreign_key => "benefit_type"
attr_accessible :account_id, :active, :attachment, :automatic_rollover, :id, :benefit_id, :benefit_type_id
attr_accessible :benefit_coverage_id, :benefit_type, :is_pretax, :benefit_coverage_period_id
attr_accessible :description, :enrollable, :link, :name, :has_plans, :has_custom_amount, :benefit_plans_attributes
validates_format_of :link, :message => "Please enter a valid URL", :with => /^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/ix
accepts_nested_attributes_for :benefit_plans, allow_destroy: true
end
class BenefitPlan < ActiveRecord::Base
belongs_to :benefit, :inverse_of => :benefit_plans
validates_presence_of :benefit
has_many :employee_benefits
has_many :benefit_coverages
attr_accessible :benefit_id, :description, :name, :benefit_coverages_attributes, :link, :attachment
accepts_nested_attributes_for :benefit_coverages, allow_destroy: true
end
class BenefitCoverage < ActiveRecord::Base
belongs_to :benefit_plan
has_many :employee_benefits
belongs_to :name_id, :class_name => "LookupTable", :foreign_key => "name"
attr_accessible :benefit_plan_id, :name, :paycheck_deduction, :percentage_split, :total_cost
accepts_nested_attributes_for :employee_benefits
end
Controller
def new
#benefit = Benefit.new
#benefit_coverage_periods = #account.benefit_coverage_periods
1.times { #benefit.benefit_plans.build(:name => 'Default Plan') }
#lookup_tables = LookupTable.where(:active => :true).find_all_by_group(30) || []
#lookup_tables1 = LookupTable.where(:active => :true).find_all_by_group(31) || []
end
def create
#benefit = Benefit.new(params[:benefit])
#lookup_tables = LookupTable.where(:active => :true).find_all_by_group(30) || []
#lookup_tables1 = LookupTable.where(:active => :true).find_all_by_group(31) || []
#benefit_coverage_periods = #account.benefit_coverage_periods
if #benefit.save
redirect_to benefits_path, :notice => 'Benefit was successfully created.'
else
render :action => "new"
end
View
= form_for #benefit, :html => { :class => "form-horizontal" }, :validate => true do |f|
= f.hidden_field :account_id, :value => #account.id
.control-group
= f.label :name, :class => "control-label"
.controls
= f.text_field :name, :class => "text_field"
.control-group
= f.label :benefit_type, "Benefit Type", :class => "control-label"
.controls
= f.collection_select :benefit_type, #lookup_tables1, :id, :title, :prompt => true
...
Error
NoMethodError - undefined method `map' for nil:NilClass:
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:364:in `options_from_collection_for_select'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:600:in `to_collection_select_tag'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:191:in `collection_select'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/form_options_helper.rb:646:in `collection_select'
15:00:09 web.1 | client_side_validations (3.2.6) lib/client_side_validations/action_view/form_builder.rb:77:in `collection
_select_with_client_side_validations'
15:00:09 web.1 | app/views/benefits/_form.html.haml:10:in `block in _app_views_benefits__form_html_haml___5210787370672092
58_70323180464100'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers/action_view_mods.rb:132:in `block (2 levels) in form_for_with_haml'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers.rb:284:in `with_tabs'
15:00:09 web.1 | haml (4.1.0.beta.1) lib/haml/helpers/action_view_mods.rb:132:in `block in form_for_with_haml'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/capture_helper.rb:40:in `block in capture'
15:00:09 web.1 | actionpack (3.2.14) lib/action_view/helpers/capture_helper.rb:187:in `with_output_buffer'
...
The stack trace tells you everything you need to know to figure this out. Let's learn to fish!
First of all "Undefined method X for Y" means "You tried to call a method X on object Y, but Y doesn't have that method". In this case, you're trying to call map on nil, and nil doesn't implement map. So we're looking for a place that we call map, and then we're going to try to figure out why the object we're calling map on could be nil.
15:00:09 web.1 | app/views/benefits/_form.html.haml:10:in `block in _app_views_benefits__form_html_haml___5210787370672092
This tells you to look on line 10 in _form.html.haml. That's where you're going to fix the issue. However, the issue happens in lib/action_view/helpers/form_options_helper.rb:364 in the options_from_collection_for_select method. It's a pretty good guess that that corresponds to this line:
= f.collection_select :benefit_type, #lookup_tables1, :id, :title, :prompt => true
If you look at the source there, you can see what it's calling. Working backwards from the top of the stack trace:
https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_view/helpers/form_options_helper.rb#L364
https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_view/helpers/form_options_helper.rb#L600
https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_view/helpers/form_options_helper.rb#L191
https://github.com/rails/rails/blob/v3.2.14/actionpack/lib/action_view/helpers/form_options_helper.rb#L646
We can see that it's calling map on the collection that is passed in. By following the stack trace back to the FormBuilder#collection_select method, we can determine exactly which parameter this is - #lookup_tables1, in your case.
Now, #lookup_tables1 is set in your new action, but it's not set in your create action, when you render action: :new. From this, we can surmise that the first time you try to save your Benefit record, the save fails and returns false. Your second branch is run there, which attempts to render the new template, but because you didn't set #lookup_tables1, the collection_select fails as it's attempting to create a select box from a nil collection.
So, you have two tasks:
Populate your #lookup_tables1 from your create method before you call render action: :new
Figure out why your Benefit record is failing to save, and fix that.
As an aside, you should update Rails to the the latest 3.x series (3.2.19 as of right now). 3.2.14 has several vulnerabilities that have been since fixed.
I'm having some trouble saving a model
class AmazonMovie < ActiveRecord::Base
attr_accessible :asin_buy, :asin_rent, :movie_id, :on_prime, :on_prime_prev, :price_buy, :price_buy_prev, :price_rent, :price_rent_prev, :url, :updated_at, :created_at
belongs_to :movie
has_many :amazon_movie_changes, :dependent => :destroy
validates :movie_id, presence: true, uniqueness: true
AWS_QPS_THRESHOLD = 1.0
before_create :init
def init
self.on_prime_prev = false
end
def update_changes
...
There's another class
class AmazonMovieChange < ActiveRecord::Base
attr_accessible :amazon_movie_id, :buy_change, :movie_id, :on_prime, :on_prime_prev, :price_buy, :price_buy_prev, :price_rent, :price_rent_prev, :prime_change, :rent_change
belongs_to :movie
belongs_to :amazon_movie
validates_presence_of :movie_id, :amazon_movie_id
....
When I created a model like the following:
=> #<AmazonMovie id: nil, movie_id: 16918, asin_buy: "B00FSTCUGA", price_buy: 7.99, price_buy_prev: nil, asin_rent: "B00FST7SX0", price_rent: 3.99, price_rent_prev: nil, on_prime: false, on_prime_prev: false, url: "http://www.amazon.com/Strongman-Stanless-Steel/dp/B...", created_at: nil, updated_at: nil>
I'm getting the following save failure:
ActiveRecord::RecordInvalid: Validation failed: Amazon movie changes is invalid
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/validations.rb:56:in `save!'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/attribute_methods/dirty.rb:33:in `save!'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `block in save!'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:208:in `transaction'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
from /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.8/lib/active_record/transactions.rb:246:in `save!'
from (irb):145
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
UPDATE:
I realized by removing the before_create :init this model saves without errors. My original intent was to default this field as false instead of NULL. Is there something wrong in my syntax? Or this is not the right way to default a field?
Models:
class User < ActiveRecord::Base
end
class Game::Session < ActiveRecord::Base
has_many :session_users, foreign_key: 'game_session_id'
has_many :users, through: :session_users
end
class Game::SessionUser < ActiveRecord::Base
belongs_to :user
belongs_to :session, foreign_key: 'game_session_id'
end
Tables:
create_table :users do |t|
...
end
create_table :game_sessions do |t|
...
end
create_table :game_session_users, :id => false do |t|
t.references :user, null: false
t.references :game_session, null: false
t.string :state, null: false
end
I have no problem creating and querying Game::SessionUser models, but I get SQL error when trying to update them.
for example:
Game::Session.first.session_users.first.update(state: 'winner')
results in this:
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: zero-length delimited identifier at or near """"
LINE 1: ...sers" SET "state" = $1 WHERE "game_session_users"."" IS NULL
^
: UPDATE "game_session_users" SET "state" = $1 WHERE "game_session_users"."" IS NULL
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:811:in `prepare'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:811:in `prepare_statement'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:772:in `exec_cache'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `block in exec_delete'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:158:in `exec_delete'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:102:in `update'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `update'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:489:in `update_record'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/locking/optimistic.rb:70:in `update_record'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/attribute_methods/dirty.rb:74:in `update_record'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/callbacks.rb:307:in `block in update_record'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:373:in `_run__995479063__update__callbacks'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/callbacks.rb:80:in `run_callbacks'
... 16 levels...
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:281:in `rollback_active_record_state!'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:269:in `save'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:230:in `block in update'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:326:in `block in with_transaction_returning_status'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in `transaction'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:323:in `with_transaction_returning_status'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/persistence.rb:228:in `update'
from (irb):1
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from C:/Ruby200/lib/ruby/gems/2.0.0/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
The db is PostgreSQL, thanks.
I fixed the issue by using this composite keys gem: http://compositekeys.rubyforge.org/ and make sure to use version 6.0.0.
You'll just need to specify the composite primary key in your model:
class Game::SessionUser < ActiveRecord::Base
belongs_to :user
belongs_to :session, foreign_key: 'game_session_id'
self.primary_keys = :user_id, :game_session_id
end
I have
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :name
t.timestamps
end
end
end
and
class Role < ActiveRecord::Base
attr_accessible :name
has_many :members, :posts
end
class Post < ActiveRecord::Base
attr_accessible :content, :title, :role_id
belongs_to :role
end
class Member < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :lockable
attr_accessible :role_id, :first_name, :last_name, :email, :password, :password_confirmation, :remember_me
end
In the Rails console or in seeds.rb, I type
Role.create(name: 'guest')
and get the error
TypeError: can't convert Symbol into Integer
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:35:in `[]'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:35:in `wrap_block_extension'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:22:in `build'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/autosave_association.rb:139:in `build'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/has_many.rb:10:in `build'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/builder/collection_association.rb:13:in `build'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations.rb:1195:in `has_many'
from /Users/ataylo9/Dropbox/Developer/hamsterdam/app/models/role.rb:3:in `<class:Role>'
from /Users/ataylo9/Dropbox/Developer/hamsterdam/app/models/role.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `block in load_file'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in `new_constants_in'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in `load_file'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in `require_or_load'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_missing_constant'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
I know I get the error because Rails wants to build the relationship for Members and Posts, but shouldn't it just make those nil. I even tried explicitly setting the arrays to nil in seeds.rb, but got the same error.
What am I failing to understand? Thanks!
UPDATED: Added Post and Member model for reference
I created the same project with the same models. And what I found out this kind of description relations occurs that error.
class Role < ActiveRecord::Base
attr_accessible :name
has_many :members, :posts
end
I tried this:
class Role < ActiveRecord::Base
attr_accessible :name
has_many :members
has_many :posts
end
class Post < ActiveRecord::Base
attr_accessible :name, :role_id
belongs_to :role
end
class Member < ActiveRecord::Base
attr_accessible :name, :role_id
belongs_to :role
end
And everything works. I don't know why but looks like has_many :posts, :members occurs the problem. But you can write in different way to resolve this issue.
jizak's answer is what helped push me in the right direction, in terms of finding the solution to my own problem. At first, I was trying to add multiple 'items' to a single has_many, as in the above example:
has_many :members, :posts
My Rails console game me the same error - "TypeError: can't convert Symbol into Integer." So I split it up onto two different lines:
has_many :members
has_many :posts
And now it works fine.
I guess I was trying to be a little too clever, thinking associations (has_many) would be similar to attr_accessor/writer/reader - where one can add multiple things to a single attr_x (or in this case, has_many). This is not the case with Rails (as far as I know) - each association needs its own individual declaration.
It looks like you assigned relations (has_many :members, :posts) which doesn't exist. Do you have such models? Do models have such relations? Can you post code of Member and Post model?
Greetings all,
I'm having a weird problem with a habtm relationship and honestly I'm beginning to think I may have stumbled upon some weird bug in rails 3. Surely I'm crazy though. I've been beating my head against the wall on this for 3 days, have googled everything under the sun I can think of and still can't come up with an answer.
Ok, the situation:
I'm creating a Rails app to replace both a Java app and a PHP app (java application and php front-end). This is going to be a phased operation with the first phase being the Rails application takes over registration and billing. In order to do this, the Rails application must create data in the databases for the Java and PHP apps. The Rails application itself is using Devise for authentication.
In database.yml I have my standard 3 databases defined and also a connection defined for the Java apps database.
Here are pieces of the model definitions for the external object (I'm just creating regular rails models to talk to the external databases):
class Pushbroom::UserAccount < ActiveRecord::Base
require 'digest/md5'
require 'base64'
establish_connection :pushbroom
set_table_name :user_account
set_primary_key :id
has_and_belongs_to_many :user_roles, :join_table => 'pb_prod.users_roles', :class_name => 'Pushbroom::UserRole', :foreign_key => 'user_account_id', :association_foreign_key => 'user_role_id'
belongs_to :user, :dependent => :destroy
attr_accessible :user_roles, :admin_notes, :enabled, :username, :password_hash, :prefStore, :accepted_tos, :do_not_contact
end
class Pushbroom::UserRole < ActiveRecord::Base
establish_connection :pushbroom
set_table_name :user_role
set_primary_key :id
has_and_belongs_to_many :user_accounts, :join_table => 'pb_prod.users_roles', :class_name => 'Pushbroom::UserAccount', :foreign_key => 'user_role_id', :association_foreign_key => 'user_account_id'
end
And finally my Rails application user object:
class User < ActiveRecord::Base
after_create :send_welcome_email
before_save :create_pushbroom_user_data
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
belongs_to :pb_user_account, :class_name => "Pushbroom::UserAccount", :foreign_key => "pb_user_account_id", :dependent => :destroy, :autosave => true
# Setup accessible (or protected) attributes for your model
attr_accessible :first_name, :last_name, :username, :dob, :email, :password, :password_confirmation, :remember_me
validates_presence_of :first_name, :last_name, :username, :dob
validates_date :dob, :on_or_after => lambda { 100.years.ago }, :on_or_after_message => "must be on or after #{100.years.ago.strftime('%m-%d-%Y')}"
validates_date :dob, :on_or_before => lambda { 13.years.ago }, :on_or_before_message => "must be on or before #{13.years.ago.strftime('%m-%d-%Y')}"
def create_pushbroom_user_data
pb_user = create_pushbroom_user
pb_user_account = create_pushbroom_user_account(pb_user)
pb_user_account.user_roles << Pushbroom::UserRole.find_by_name('user')
self.pb_user_account = pb_user_account
end
def create_pushbroom_user
pb_user = Pushbroom::User.new
pb_user.attributes = self.attributes.slice(
"email",
"first_name",
"last_name",
"dob")
pb_user
end
def create_pushbroom_user_account(pb_user)
pb_user_account = Pushbroom::UserAccount.new
pb_user_account.enabled = true
pb_user_account.password_hash = Pushbroom::UserAccount.create_password_digest(#plaintext_password, self.username)
pb_user_account.username = self.username
pb_user_account.user = pb_user
pb_user_account
end
Seems like it should be pretty vanilla. The ONLY weirdness here is that they aren't in the native rails database and one of the fields is named funny in the relations table.
So here's a rails console session where I create a rails user, call the method to create the external objects, then try to save:
ruby-1.9.2-p180 :001 > def user_fred
ruby-1.9.2-p180 :002?> {
ruby-1.9.2-p180 :003 > :first_name => "Fred",
ruby-1.9.2-p180 :004 > :last_name => "Flinstone",
ruby-1.9.2-p180 :005 > :username => "fflint",
ruby-1.9.2-p180 :006 > :dob => "1986-06-01",
ruby-1.9.2-p180 :007 > :email => "fred#mydomain.org",
ruby-1.9.2-p180 :008 > :password => "badpass"
ruby-1.9.2-p180 :009?> }
ruby-1.9.2-p180 :010?> end
=> nil
ruby-1.9.2-p180 :011 > user = User.new(user_fred)
=> #<User id: nil, email: "fred#mydomain.org", encrypted_password: "$2a$10$IiEOEoSnXIrP7VJAQYckfOVXuzm7Y5ZGo20ayLpSkHhz...", reset_password_token: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, created_at: nil, updated_at: nil, first_name: "Fred", last_name: "Flinstone", username: "fflint", dob: "1986-06-01", pb_user_account_id: nil>
ruby-1.9.2-p180 :012 > user.create_pushbroom_user_data
=> #<Pushbroom::UserAccount id: nil, created_by: nil, created_at: nil, updated_by: nil, updated_at: nil, admin_notes: nil, enabled: true, username: "fflint", password_hash: "blah blah", user_id: nil, prefStore: nil, accepted_tos: nil, do_not_contact: nil>
ruby-1.9.2-p180 :013 > user.pb_user_account.user_roles
=> [#<Pushbroom::UserRole id: 1, created_by: "script", created_at: "2008-11-10 12:10:44", updated_by: "script", updated_at: "2008-11-10 12:10:44", admin_notes: "", name: "user", description: "Generic User Role", conditional: false>]
ruby-1.9.2-p180 :014 > user.save!
NoMethodError: undefined method `relation' for nil:NilClass
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activesupport- 3.0.5/lib/active_support/whiny_nil.rb:48:in `method_missing'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/arel- 2.0.9/lib/arel/insert_manager.rb:22:in `insert'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/arel- 2.0.9/lib/arel/crud.rb:26:in `insert'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord- 3.0.5/lib/active_record/associations/has_and_belongs_to_many_association.rb:76:in `insert_record'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord- 3.0.5/lib/active_record/associations/association_proxy.rb:151:in `send'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:306:in `block in save_collection_association'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `block in method_missing'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `block in method_missing'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `each'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_proxy.rb:216:in `method_missing'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/associations/association_collection.rb:431:in `method_missing'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:297:in `save_collection_association'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/autosave_association.rb:163:in `block in add_autosave_association_callbacks'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activesupport-3.0.5/lib/active_support/callbacks.rb:415:in `_run_create_callbacks'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:281:in `create'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/persistence.rb:246:in `create_or_update'
... 18 levels...
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/callbacks.rb:277:in `create_or_update'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/persistence.rb:56:in `save!'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/validations.rb:49:in `save!'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/attribute_methods/dirty.rb:30:in `save!'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in `block in save!'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord- 3.0.5/lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:207:in `transaction'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/activerecord-3.0.5/lib/active_record/transactions.rb:245:in `save!'
from (irb):14
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/railties-3.0.5/lib/rails/commands/console.rb:44:in `start'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/railties-3.0.5/lib/rails/commands/console.rb:8:in `start'
from /Users/gander/.rvm/gems/ruby-1.9.2-p180#sms2/gems/railties-3.0.5/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'ruby-1.9.2-p180 :015 >
If I remove the role assignment, everything is just peachy (finds, saves, destroys, etc), but the second I try to save roles everything blows sky-high with this message that, frankly, I don't get. It knows its got the roles, there is no nil object that I can tell. . .and basically if I wasn't already bald I'd be pulling my hair out ; )
Any insight into this is EXTREMELY appreciated!
Gerald
P.S. Also asked here http://railsforum.com/viewtopic.php?id=43647 Will duplicate answer if found.
After beating myself silly for 4 days on this I finally found the problem: Rails (habtm) doesn't have the ability to determine the database to use for external relation tables. I also found the answer, and it doesn't even smell bad! There's a whole thread on the process here: http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/c5655d0442039ccd
The answer? has_many :through - something I'd never taken much of a look at, but it is actually a pretty nice feature (even in other circumstances).
Basically this just allows me to create a model class which represents the relationship. And since I have a model class for it I can explicitly specify the database to connect to.
For posterity sake, here's the code:
class Pushbroom::UsersRolesRelationship < ActiveRecord::Base
establish_connection :pushbroom
set_table_name :users_roles
belongs_to :user_account
belongs_to :user_role
end
class Pushbroom::UserAccount < ActiveRecord::Base
establish_connection :pushbroom
set_table_name :user_account
set_primary_key :id
has_many :users_roles_relationships
has_many :user_roles, :through => :users_roles_relationships, :source => :user_role
end
class Pushbroom::UserRole < ActiveRecord::Base
establish_connection :pushbroom
set_table_name :user_role
set_primary_key :id
has_many :users_roles_relationships
has_many :user_accounts, :through => :users_roles_relationships, :source => :user_account
end
And is used thusly:
def add_subscription_plan_roles_to_pb_user_account(pb_user_account)
roles_granted = pb_user_account.user.subscriptions.first.subscription_plan.roles_granted
pb_user_account.user_roles = roles_granted
end
Thanks a ton folks for helping me get this train moving again! All my tests are passing and it seems to be working, but if you see something wrong, please do still let me know.
Thanks!
Gerald
Try doing some manual saves of the objects created in various places, such as the create_pushbroom_user_account(pb_user) method. I have had some issues in the past when relying on the "autosave" system.