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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:06:18+00:00 2026-05-14T01:06:18+00:00

I have an ExpenseType object that I have created with the following migration: class

  • 0

I have an ExpenseType object that I have created with the following migration:

class CreateExpenseTypes < ActiveRecord::Migration
  def self.up
    create_table :expense_types do |t|
      t.column :name, :string, :null => false
      t.timestamps
    end
  end

I can see the table name is the pluralised expense_types. My question is, how do I reference this type in a belongs_to relationship?

Is it:

belongs_to :expensetype

or is it

   belongs_to :expense_type

I do not seem able to set it up correctly.

Also, how would I reference the object if it was contained in an expense object like this:

class Expense < ActiveRecord::Base
  belongs_to :expense_type
end

would it be:

expense.expense_type

or

expense.expensetype

Cheers

  • 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-05-14T01:06:19+00:00Added an answer on May 14, 2026 at 1:06 am

    You are correct with:

    # app/models/expense.rb
    class Expense < ActiveRecord::Base
      belongs_to :expense_type
    end
    

    You can also setup ExpenseType as follows:

    # app/models/expense_type.rb
    class ExpenseType < ActiveRecord::Base
      has_many :expenses
    end
    

    Also, if expense is an instance of Expense, you can access the expense_type as:

    expense.expense_type
    

    For more information

    see ActiveRecord::Associations::ClassMethods

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

Sidebar

Related Questions

have the following: // watch the fields [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextChange:) name:UITextFieldTextDidChangeNotification object:textField1]; and
have written this little class, which generates a UUID every time an object of
Have a simple contact us XPage created. Have server side validation in place that
Have the following code: String s= v.request(engine/?key=, P4z72NmBa91&method=load); JSONParser parser = new JSONParser(); Object
I have 2 tables Table1 with columns [BId,Name,Amount] Table2 with columns [CId,BId, ExpenseType,Expense]. BId
Have I in some way delete this Task or it is self-destroyed? Task.Factory.StartNew(() =>
Have such user_session model: class UserSession < Authlogic::Session::Base logout_on_timeout true # default if false
Have a simple form that has a PictureBox in one location. I want to
Have two tables say ABC and XYZ and contain one column which data will
I'm aware that questions have been asked and answered about the Android soft keyboard

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.