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

On 22 September 2010 21:33, Al Rowan [email protected] wrote:

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.-][email protected][\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


class Group < ActiveRecord::Base

validates :groupname, :presence => true

belongs_to :user
has_many :students
end


user = User.new
=> #<User id: nil, email: nil, hashed_password: nil, subject_id: nil,
created_at: nil, updated_at: nil>


group = Group.new
=> #<Group id: nil, groupname: nil, user_id: nil, course_id: nil,
created_at: nil, updated_at: nil>


WHAT THE HELL. i just did user.groups and it worked…i didnt change
anything! now im even more confused.

activesupport (3.0.0) lib/active_support/whiny_nil.rb:48:in
method_missing' app/controllers/groups_controller.rb:8:increate’
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:insend_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:inprocess_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:inrun_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:inprocess_action’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' activesupport (3.0.0) lib/active_support/notifications/instrumenter.rb:21:ininstrument’
activesupport (3.0.0) lib/active_support/notifications.rb:52:in
instrument' actionpack (3.0.0) lib/action_controller/metal/instrumentation.rb:29:inprocess_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:inprocess’
actionpack (3.0.0) lib/abstract_controller/rendering.rb:40:in process' actionpack (3.0.0) lib/action_controller/metal.rb:133:indispatch’
actionpack (3.0.0) lib/action_controller/metal/rack_delegation.rb:14:in
dispatch' actionpack (3.0.0) lib/action_controller/metal.rb:173:inaction’
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:indispatch’
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:incall’
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:inoptimized_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:incall’
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:incall’
actionpack (3.0.0) lib/action_dispatch/middleware/head.rb:14:in call' rack (1.2.1) lib/rack/methodoverride.rb:24:incall’
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:incall’
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:incall’
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:incache’
activerecord (3.0.0) lib/active_record/query_cache.rb:12:in cache' activerecord (3.0.0) lib/active_record/query_cache.rb:31:incall’
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:incall’
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:incall’
rack (1.2.1) lib/rack/sendfile.rb:107:in call' actionpack (3.0.0) lib/action_dispatch/middleware/remote_ip.rb:48:incall’
actionpack (3.0.0)
lib/action_dispatch/middleware/show_exceptions.rb:46:in call' railties (3.0.0) lib/rails/rack/logger.rb:13:incall’
rack (1.2.1) lib/rack/runtime.rb:17:in call' activesupport (3.0.0) lib/active_support/cache/strategy/local_cache.rb:72:incall’
rack (1.2.1) lib/rack/lock.rb:11:in call' rack (1.2.1) lib/rack/lock.rb:11:insynchronize’
rack (1.2.1) lib/rack/lock.rb:11:in call' actionpack (3.0.0) lib/action_dispatch/middleware/static.rb:30:incall’
railties (3.0.0) lib/rails/application.rb:168:in call' railties (3.0.0) lib/rails/application.rb:77:insend’
railties (3.0.0) lib/rails/application.rb:77:in method_missing' railties (3.0.0) lib/rails/rack/log_tailer.rb:14:incall’
rack (1.2.1) lib/rack/content_length.rb:13:in call' rack (1.2.1) lib/rack/handler/webrick.rb:52:inservice’
/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:inrun’
/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:instart’
/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:instart’
/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:instart’
/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:instart’
rack (1.2.1) lib/rack/handler/webrick.rb:13:in run' rack (1.2.1) lib/rack/server.rb:213:instart’
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:intap’
railties (3.0.0) lib/rails/commands.rb:27
script/rails:6:in `require’
script/rails:6

On 22 September 2010 21:54, Al Rowan [email protected] wrote:

                  :if => :password_required?

end

created_at: nil, updated_at: nil>


WHAT THE HELL. i just did user.groups and it worked…i didnt change
anything! now im even more confused.

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.-][email protected][\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,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs