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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:01:02+00:00 2026-06-14T19:01:02+00:00

I have this class: class User include Mongoid::Document field :revenues, :type => Integer, :default

  • 0

I have this class:

class User
  include Mongoid::Document
  field :revenues, :type => Integer, :default => nil
  attr_accessible :revenues
  #now method
  def revenues
   return 1
  end
end

Why in console I get 1 instead nil?

1.9.3-p125 :002 > u.revenues
 => 1 

Which has priority, the method or the field? How can I created a method with the same features that a field?

  • 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-14T19:01:04+00:00Added an answer on June 14, 2026 at 7:01 pm

    The field macro is defined in Mongoid::Document. It is neither a syntatic feature from Ruby nor from Rails.

    What’s happening with your code is the following:

    1. The field function creates for you some methods, one of them is called revenues.
    2. When you create another method called revenues, you are in effect overwriting the previously defined method, therefore making it useless.

    Short answer: I don’t understand a zip about Mongoid, but chances are that your field still exists even after you defined oce again a method named revenues. The only drawback is that you cannot access it by calling myUser.revenues anymore.

    Try to make a test: access your field with the notation some_user[:revenues] and see what happen 🙂

    Best regards

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

Sidebar

Related Questions

I have 2 model: class User include Mongoid::Document field :email, :type => String, :null
I have a model like this: class Lesson include Mongoid::Document field :title, :type =>
I have a relationship like this class User include Mongoid::Document include Mongoid::Timestamps embeds_one :setting
I have the following models class Track include Mongoid::Document field :artist, type: String field
I have this on my model Class User include Mongoid::Document scope :created_between, lambda {
Assume we have something like: class Company include Mongoid::Document has_many :users field :name, type:
I have the following models: class User include Mongoid::Document embeds_many :user_missions attr_accessible :user_missions_attributes accepts_nested_attributes_for
I have these relationships: class User include Mongoid::Document has_many :orders, :class_name => 'Order', :inverse_of
I have three Models: User, Picture, and Like where: class Picture include Mongoid::Document embeds_many
I have a contact info class defined like this: class ContactInfo include Mongoid::Document validates_presence_of

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.