has_many - undefined method!

Hi all,

still new at this lol, trying to create a one to many relationship

i have:

group.rb [

belongs_to :user ]

user.rb [

has_many :groups

]

but if i do "user.groups" it just throws up: "NoMethodError: undefined method 'groups'.."

again thanks for any help! Al

Hi all,

still new at this lol, trying to create a one to many relationship

i have:

group.rb [

belongs_to :user ]

user.rb [

has_many :groups

]

Could you show us the code for the models (leave out any methods you have added, just the class definition and bits you put at the beginning of the class.

but if i do "user.groups" it just throws up: "NoMethodError: undefined method 'groups'.."

Do the users and groups tables exist in the database? If so what fields do they contain?

Show us the code around the failure and the stack trace please.

Colin

class User < ActiveRecord::Base   attr_accessor :password

  validates :email, :uniqueness => true,                     :length => { :within => 5..50 },                     :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }   validates :password, :confirmation => true,                        :length => { :within => 4..20 },                        :presence => true,                        :if => :password_required?

  has_and_belongs_to_many :subjects   has_one :profile   has_many :classes, :dependent => :nullify   has_many :courses, :dependent => :nullify   has_many :works   has_many :units   has_many :groups   before_save :encrypt_new_password ..... end

activesupport (3.0.0) lib/active_support/whiny_nil.rb:48:in `method_missing' app/controllers/groups_controller.rb:8:in `create' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.0.0) lib/action_controller/metal/implicit_render.rb:4:in `send_action' actionpack (3.0.0) lib/abstract_controller/base.rb:150:in `process_action' actionpack (3.0.0) lib/action_controller/metal/rendering.rb:11:in `process_action' actionpack (3.0.0) lib/abstract_controller/callbacks.rb:18:in `process_action' activesupport (3.0.0) lib/active_support/callbacks.rb:435:in `_run__1879433143__process_action__1623385099__callbacks' activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `send' activesupport (3.0.0) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks' activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `send' activesupport (3.0.0) lib/active_support/callbacks.rb:93:in `run_callbacks' actionpack (3.0.0) lib/abstract_controller/callbacks.rb:17:in `process_action' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:30:in `process_action' activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument' activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:in `instrument' activesupport (3.0.0) lib/active_support/notifications.rb:52:in `instrument' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:29:in `process_action' actionpack (3.0.0) lib/action_controller/metal/rescue.rb:17:in `process_action' actionpack (3.0.0) lib/abstract_controller/base.rb:119:in `process' actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:in `process' actionpack (3.0.0) lib/action_controller/metal.rb:133:in `dispatch' actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' actionpack (3.0.0) lib/action_controller/metal.rb:173:in `action' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `call' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:62:in `dispatch' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:27:in `call' rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `call' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `recognize' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:82:in `optimized_each' rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize' rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call' actionpack (3.0.0) lib/action_dispatch/routing/route_set.rb:492:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:in `call' rack (1.2.1) lib/rack/methodoverride.rb:24:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/params_parser.rb:21:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/flash.rb:182:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/cookies.rb:287:in `call' activerecord (3.0.0) lib/active_record/query_cache.rb:32:in `call' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache' activerecord (3.0.0) lib/active_record/query_cache.rb:12:in `cache' activerecord (3.0.0) lib/active_record/query_cache.rb:31:in `call' activerecord (3.0.0) lib/active_record/connection_adapters/abstract/connection_pool.rb:355:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:46:in `call' activesupport (3.0.0) lib/active_support/callbacks.rb:415:in `_run_call_callbacks' actionpack (3.0.0) lib/action_dispatch/middleware/callbacks.rb:44:in `call' rack (1.2.1) lib/rack/sendfile.rb:107:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call' railties (3.0.0) lib/rails/rack/logger.rb:13:in `call' rack (1.2.1) lib/rack/runtime.rb:17:in `call' activesupport (3.0.0) lib/active_support/cache/strategy/local_cache.rb:72:in `call' rack (1.2.1) lib/rack/lock.rb:11:in `call' rack (1.2.1) lib/rack/lock.rb:11:in `synchronize' rack (1.2.1) lib/rack/lock.rb:11:in `call' actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:in `call' railties (3.0.0) lib/rails/application.rb:168:in `call' railties (3.0.0) lib/rails/application.rb:77:in `send' railties (3.0.0) lib/rails/application.rb:77:in `method_missing' railties (3.0.0) lib/rails/rack/log_tailer.rb:14:in `call' rack (1.2.1) lib/rack/content_length.rb:13:in `call' rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:104:in `service' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/httpserver.rb:65:in `run' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:173:in `start_thread' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:162:in `start_thread' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:95:in `start' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `each' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:92:in `start' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:23:in `start' /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/webrick/server.rb:82:in `start' rack (1.2.1) lib/rack/handler/webrick.rb:13:in `run' rack (1.2.1) lib/rack/server.rb:213:in `start' railties (3.0.0) lib/rails/commands/server.rb:65:in `start' railties (3.0.0) lib/rails/commands.rb:30 railties (3.0.0) lib/rails/commands.rb:27:in `tap' railties (3.0.0) lib/rails/commands.rb:27 script/rails:6:in `require' script/rails:6

Were you running it from the console when it failed, or from code? If from the console I wonder whether the group model was not loaded or something along those lines. Perhaps by doing Group.new you loaded it so all is well. I suggest pressing on with some code (preceded by writing tests of course) and see how it goes.

Colin

Al Rowan wrote:

class User < ActiveRecord::Base   attr_accessor :password

  validates :email, :uniqueness => true,                     :length => { :within => 5..50 },                     :format => { :with => /^[^@][\w.-]+@[\w.-]+[.][a-z]{2,4}$/i }

[...]

Separate issue, but a pet peeve of mine: your e-mail validation regexp is *incorrect*. It will reject lots of valid e-mail addresses (including anything with pluscoding). Valid e-mail addresses can take far more forms than most people realize, so that the only correct e-mail validation regexps I'm aware of are about a page in length.

With that in mind, then, I recommend not doing this kind of format checking on e-mail addresses. If you must use a regexp, just use something like /@.+\./ , or simply forget the regexp and e-mail an activation code to the address as a means of verifying it.

Best,