Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:21:09+00:00 2026-06-15T10:21:09+00:00

hey I’m having a little trouble with this function class User < ActiveRecord::Base belongs_to

  • 0

hey I’m having a little trouble with this function

class User < ActiveRecord::Base
  belongs_to :group
end

class Group < ActiveRecord::Base
  has_many :users
  attr_accessible :name
  validates_presence_of :name

  before_destroy :set_default_group

  private
  def set_default_group
    self.users.each do |u| 
      puts u # prints out <User:0x007fd678a85cb8>
      u.group = self.first
      u.save    
    end
  end
end

it gives me this error NoMethodError: undefined method `group’ for #Group id: 18, so what is wrong with my definition?

the error message

    NoMethodError: undefined method `group' for #<Group id: 18>
from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/attribute_methods.rb:407:in `method_missing'
    from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/attribute_methods.rb:149:in `method_missing'
    from /Users/.../Developer/rails/.../app/models/group.rb:12:in `block in set_default_group'
    from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:89:in `each'
    from /Users/.../.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
    from /Users/.../Developer/rails/.../app/models/group.rb:10:in `set_default_group'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:21:09+00:00Added an answer on June 15, 2026 at 10:21 am

    Your error doesn’t make sense, since you’re calling #group on user and not on group.

    However, I believe that

    u.group = self.first
    

    will not work as you intend it to, since #first is a class method, and self here is an instance. Try:

    u.group = self.class.first
    

    or simply:

    u.group = Group.first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey guys Im attempting to sort a linked list and having trouble with this
Hey guys this is my html code: <div class=nakupy> <li class=icn_kategorie><a href=#>Nákupy</a> <div class=sub_menu>
hey guys having this really simple problem but cant seem to figure out have
Hey there, Now im working this base on dmitko tutorial on extending django-registration post
hey guys I have the follow code in js: $(document).ready(function(){ $(.replyLink).click(function(){ $(#form-to-+this.id).html(htmlForm()).toggle(500); return false;
Hey I'm having a problem trying to figure this out: Lets start out with
Hey having some trouble trying to maintain transparency on a png when i create
Hey I need a little help with unsigned char bit access. I need to
Hey I thought about writing a function that prompts me in certain situations (perl
Hey everyone I have a basic search function here that allows me to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.