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

  • SEARCH
  • Home
  • 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 8110109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T01:38:27+00:00 2026-06-06T01:38:27+00:00

I have the following class: class Group < ActiveRecord::Base has_many :users belongs_to :leader, :foreign_key

  • 0

I have the following class:

class Group < ActiveRecord::Base

  has_many   :users 
  belongs_to :leader, :foreign_key => "leader_id", :class_name => "User"

  def get_leader!
    if leader.try(:is_active?)
      return leader
    else
      leader = users.active.sort_by { |u| u.completed_sessions }.last
      save! # TODO: this does not save the new leader ... no idea why
      return leader
    end
  end    

end

When I call the get_leader! method it always returns the correct leader but fails to update the foreign_key ‘leader_id’ for the object.

As you can see from my actual code comment, I have no idea why. When I go to the console and explicitly set the leader for a group everything seems to work fine.

Does anyone understand why this would be?

(Incidentally, the purpose of the method is to update the leader of a group when the previous leader has become inactive. The new leader is the one with the most logins.)

  • 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-06T01:38:28+00:00Added an answer on June 6, 2026 at 1:38 am

    Try adding self in front of leader:

    self.leader = users.active.sort_by{|u| u.completed_sessions}.last
    save!
    

    This is necessary in Ruby assignments, as Ruby doesn’t know if leader is a variable local to that method, or an assignment method on the object self.

    Also, if completed_sessions is a database column, I’d consider using this code to pull the correct user:

    self.leader = users.active.order("completed_sessions DESC").first
    

    This will sort the database using your database query, instead of pulling all records and having Ruby then sort them – more efficient when your user database gets bigger.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following model structure: class Group < ActiveRecord::Base has_many :group_products, :dependent =>
I have several classes associated as following: class User < ActiveRecord::Base has_many :posts, :dependent
I have the following models to associate users with roles: class User < ActiveRecord::Base
I have the following models. class Post < ActiveRecord::Base has_many :timeline_items, :as=>:subject define_index do
I have the following code: # app/models/part.rb class Part < ActiveRecord::Base has_many :lots #
I have the following model in my Django app: class Group(models.model): name=models.CharField(max_length=30) users=Models.ManyToManyField(User) In
I have a the following classes class Service < ActiveRecord::Base has_many :incidents end class
I have the following scenario: class Task { int id; Group group; User user;
I have the following two simple models for users and user groups: class User
Is this doable? I have the following scope: class Thing < ActiveRecord::Base scope :with_tag,

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.