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 7711007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:16:03+00:00 2026-06-01T01:16:03+00:00

I have HABTM models Client and Book . Client model has a bookshelf_color attribute

  • 0

I have HABTM models Client and Book. Client model has a bookshelf_color attribute to indicate whether a client has a full set of books, part of them or none of them. Once the books on a bookshelf change, a callback set_bookshelf_color is supposed to reflect the change.

The question is why do I have to prefix bookshelf_color assignment in a private callback below with "self." to make it to work (as it does not otherwise)?

class Client < ActiveRecord::Base

  has_and_belongs_to_many :books, autosave: true, uniq: true,
    after_add: :set_bookshelf_color, after_remove: :set_bookshelf_color

  attr_accessible :id, :book_ids, :bookshelf_color

  private

    def set_bookshelf_color(book)
      if Book.pluck(:abbr).map{|b| books.map(&:abbr).map(&:to_s).include?(b.to_s)}.all?
        self.bookshelf_color = "green"
      elsif Book.pluck(:abbr).map{|b| books.map(&:abbr).map(&:to_s).include?(b.to_s)}.any?
        self.bookshelf_color = "yellow"
      else
        self.bookshelf_color = "red"
      end
    end
  # /private
end
  • 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-01T01:16:04+00:00Added an answer on June 1, 2026 at 1:16 am

    Remember, your model properties are just instance variables behind the scenes – all we’re doing when we interact with them is calling the getter and setter methods set up by attr_accessor. When we write instance.bookshelf_color = "red" we’re sending the method bookshelf_color=, with the argument "red", to the receiver, instance.

    Cool. What happens when we write just bookshelf_color = "red"?

    In Ruby, bareword assignment is used to define local variables. Putting name = "value" in a method definition will define name in the local scope, rather than calling the name= method on self, even if that method exists.

    class Foo
      attr_accessor :bar
    
      def set_bar(val)
        bar = val
      end
    end
    
    f = Foo.new
    f.set_bar "baz"
    f.bar # still nil
    

    If I had specified an explicit receiver, self.bar = val, Ruby would have known I wanted to send self the method bar=, which would have had the desired result.

    There’s some good further reading on the joys of self, and why you should use it, over on Joe Yates’ Blog.

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

Sidebar

Related Questions

I have a pretty simple HABTM set of models class Tag < ActiveRecord::Base has_and_belongs_to_many
I have a Ruby on Rails model that has quite a few HABTM relationships.
I have 2 models with a joinging table (bowsers_profiles). (HABTM) ie. model Profile has_and_belongs_to_many
I have a working HABTM association between the models Posts and Users... the posts_users
I have 2 models which are connected via a HABTM association, in the same
I have a model Task which will HABTM many TaskTargets. When it comes to
I have the following relationships set up: A HABTM B B belongsTo C C
I have two models, Leads and courses, Leads HABTM courses. I want to list
I have two models, users and themes, that I'm currently joining in a HABTM
I have two models, associated with a HABTM (actually using has_many :through on both

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.