Undefined method for friendly_id implementation - ruby-on-rails-3

This is what my Plan.rb looks like:
class Plan < ActiveRecord::Base
has_many :users
has_friendly_id :plan, :use_slug => true
end
User.rb
class User < ActiveRecord::Base
#There is other stuff in here, but I stripped it out for brevity purposes.
has_and_belongs_to_many :projects
has_many :stages
has_many :uploads
has_many :comments
has_many :assignments
has_many :roles, :through => :assignments
belongs_to :plan
end
This is the error message I am getting:
'ruby-1.9.2-p0 > Plan.create(:name => "First_Test", :storage => "100")
NoMethodError: undefined method `plan' for #<Plan:0x00000102b8a2d8>
/gemms/ruby-1.9.2-p0/gems/activemodel-3.0.3/lib/active_model/attribute_methods.rb:364:in `method_missing'
/gemms/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods.rb:46:in `method_missing'
/gems/ruby-1.9.2-p0/gems/friendly_id-3.2.0/lib/friendly_id/slugged.rb:82:in `slug_text'
/gems/ruby-1.9.2-p0/gems/friendly_id-3.2.0/lib/friendly_id/active_record_adapter/slugged_model.rb:49:in `build_a_slug'
/gems/ruby-1.9.2-p0/gems/activesupport-3.0.3/lib/active_support/callbacks.rb:419:in `_run_save_callbacks'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/callbacks.rb:277:in `create_or_update'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/persistence.rb:39:in `save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/validations.rb:43:in `save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block (2 levels) in save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:289:in `block in with_transaction_returning_status'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:204:in `transaction'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:287:in `with_transaction_returning_status'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:237:in `block in save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:248:in `rollback_active_record_state!'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/transactions.rb:236:in `save'
/gems/ruby-1.9.2-p0/gems/activerecord-3.0.3/lib/active_record/base.rb:498:in `create'
from (irb):3
/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:44:in `start'
/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands/console.rb:8:in `start'
/gems/ruby-1.9.2-p0/gems/railties-3.0.3/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Not sure what I am doing wrong here, but I can't create any new 'plans'.

It looks like your database doesn't have a plan column in your plans table. In addition to adding the has_friendly_id statement to your model, did you create and run a migration to add the plan field to your database?

You can also get this error (undefined method `has_friendly_id') if you have upgraded friendly_id from Version 3 to 4.
Command 'has_friendly_id' has been replaced by 'friendly_id' - see how to upgrade here:
http://rubydoc.info/github/norman/friendly_id/master/file/WhatsNew.md
Note: You'll also need to create a migration to add a new slug column to your table.

Related

Rails ActiveRecard validation fail

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?

update with has_many through SQL Error

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

ckeditor in Rails 3.0.9

i have my project with Rails 3.0.9 and i added the CKeditor 3.6.3, in development works fine, and in Staging in Heroku works fine too, but in Production crash the app.
This are the configuration i have to production:
config/application.rb
config.autoload_paths += %W(#{config.root}/app/models/ckeditor)
config/environments/production.rb
#config.assets.precompile += Ckeditor.assets
I have commented the line because config.assets is not available until Rails 3.1, but i don't want to upgrade Rails in this moment.
app/models/ckeditor/attachment_file.rb
class Ckeditor::AttachmentFile < Ckeditor::Asset
has_attached_file :data,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:url => ":s3_domain_url"
validates_attachment_size :data, :less_than => 100.megabytes
validates_attachment_presence :data
def url_thumb
#url_thumb ||= Ckeditor::Utils.filethumb(filename)
end
end
app/models/ckeditor/picture.rb
class Ckeditor::Picture < Ckeditor::Asset
has_attached_file :data, :styles => { :content => '800>', :thumb => '118x100#' },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:path => ":picture/:id/:style.:extension",
:url => ":s3_domain_url"
validates_attachment_size :data, :less_than => 2.megabytes
validates_attachment_presence :data
def url_content
url(:content)
end
end
And i received the next error:
/home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/aws-s3-0.6.2/lib/aws/s3/extensions.rb:206:in `const_missing_from_s3_library': uninitialized constant Ckeditor::Asset (NameError)
from /home/jorge/trabajos/blanet/app/models/ckeditor/attachment_file.rb:1:in `<top (required)>'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/engine.rb:138:in `block (2 levels) in eager_load!'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/engine.rb:137:in `each'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/engine.rb:137:in `block in eager_load!'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/engine.rb:135:in `each'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/engine.rb:135:in `eager_load!'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/application.rb:108:in `eager_load!'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/application/finisher.rb:41:in `block in <module:Finisher>'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `instance_exec'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/initializable.rb:25:in `run'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/initializable.rb:50:in `block in run_initializers'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `each'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/initializable.rb:49:in `run_initializers'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/application.rb:134:in `initialize!'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/application.rb:77:in `method_missing'
from /home/jorge/trabajos/blanet/config/environment.rb:5:in `<top (required)>'
from /home/jorge/trabajos/blanet/config.ru:3:in `block in <main>'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/builder.rb:46:in `instance_eval'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/builder.rb:46:in `initialize'
from /home/jorge/trabajos/blanet/config.ru:1:in `new'
from /home/jorge/trabajos/blanet/config.ru:1:in `<main>'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/builder.rb:35:in `eval'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/builder.rb:35:in `parse_file'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/server.rb:162:in `app'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/server.rb:253:in `wrapped_app'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/rack-1.2.3/lib/rack/server.rb:204:in `start'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/commands/server.rb:65:in `start'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/commands.rb:30:in `block in <top (required)>'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/commands.rb:27:in `tap'
from /home/jorge/.rvm/gems/ruby-1.9.2-p290#rails3/gems/railties-3.0.9/lib/rails/commands.rb:27:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
Any one can help me?
Thanks for advance.
It looks like it can't load the parent class for your attachments. Is Ckeditor::Asset defined somewhere? Is it being loaded? Maybe you need a require ckeditor/asset somewhere?

Getting an error with custom validation in a Rails 3 ActiveModel

I'm trying to use the Date Validator Gem but I am running into an error and I'm not sure if it's because the model isn't Active Record or not (I've seen people suggest that validation is a bit funky in ActiveModel when not in ActiveRecord).
I am using Ruby 1.9.2 & Rails 3.0.7. I've attached both the class and the error below.
Thanks in advance for any help!
Class
require 'ice_cube'
require 'active_support'
require 'active_support/time_with_zone'
require 'ostruct'
require 'tzinfo'
require 'active_model'
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
class Schedule
attr_accessor :yaml, :repeat, :interval, :interval_unit, :start_time, :start_date, :end_date, :end_time, :ends, :on, :after, :monday, :tuesday, :wednesday, :thursday, :friday, :saturday, :sunday
validates_presence_of :repeat, :start_date, :start_time, :end_date, :end_time, :ends
validates_presence_of :interval, :interval_unit, :if => :ends_never?
validates_presence_of :on, :if => :ends_on_date?
validates_numericality_of :after, :greater_than => 0, :if => :ends_after_recurrences?
validates :start_date, :date => { :after => Time.now }
validates :end_date, :date => { :after => :start_date }
validates :on, :date => { :after => :end_date }
def initialize(attributes = {})
end
def persisted?
false
end
private
def parse_schedule
if :repeat == 0
get_repeatable
end
end
def parse_yaml
end
def ends_on_date?
return :ends == "on"
end
def ends_never?
return :ends == "never"
end
def ends_after_recurrences?
return :ends == "after"
end
end
Error from Rails Console
ruby-1.9.2-p180 :001 > s = Schedule.new
NoMethodError: undefined method `new' for DateValidator:Module
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:70:in `block in validates_with'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:69:in `each'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/with.rb:69:in `validates_with'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:90:in `block in validates'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:83:in `each'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.7/lib/active_model/validations/validates.rb:83:in `validates'
from /Users/chance/Sites/EatingNow/app/models/schedule.rb:16:in `<class:Schedule>'
from /Users/chance/Sites/EatingNow/app/models/schedule.rb:10:in `<top (required)>'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `load'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:454:in `block in load_file'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:596:in `new_constants_in'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:453:in `load_file'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:340:in `require_or_load'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:491:in `load_missing_constant'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:183:in `block in const_missing'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `each'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/activesupport-3.0.7/lib/active_support/dependencies.rb:181:in `const_missing'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/rspec-expectations-2.5.0/lib/rspec/expectations/backward_compatibility.rb:6:in `const_missing'
from (irb):1
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:44:in `start'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands/console.rb:8:in `start'
from /Users/chance/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.7/lib/rails/commands.rb:23:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
You have included/extended the top-level namespace. Make sure the includes/extends are IN the class.
class Schedule
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming

How to properly use Factory_girl and has_many 'through' models

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.