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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:22:59+00:00 2026-06-17T07:22:59+00:00

I’m having to build a rails app around a legacy PostgreSQL database that doesn’t

  • 0

I’m having to build a rails app around a legacy PostgreSQL database that doesn’t conform to “the Rails way” in the sense that plurality is not possible — and I’m not sure if that’s what is tripping me up or what’s tripping me up is that I’ve been away from Rails for too many months.

According to the Rails Guide, when you declare belongs_to on a model, four methods will be added to each instance of that model. In the Guide example, adding belongs_to :customer on the Order table adds: customer, customer=, build_customer, and create_customer.

I’m not seeing that in my inspection. Did I do something wrong or miss a step?

My two tables look like this:

class Zzaddress < ActiveRecord::Base
   set_table_name 'zzaddress'
   has_many :zzpersons, :class_name => 'Zzperson'
end

class Zzperson < ActiveRecord::Base
   set_table_name 'zzperson'
   belongs_to :zzaddress, :class_name => 'Zzaddress', :foreign_key => "zzaddress_id"
end

Now if I retrieve an address like this:

myaddr = Zzaddress.where( :id => 972 )

I get a result back as I expect.

When I try to access any person assigned to that address, like this:

myaddr.zzpersons

I get the beloved

NoMethodError: undefined method `zzpersons' for #<ActiveRecord::Relation:0x103c015d0>

Sure enough, if I look at the methods for the model, it’s not there either:

myaddr.methods.sort

Spew forth lots of methods, and nothing much down in the zz range 😉

If I do it without associations it works as you would expect:

grp = Zzperson.where( :zzaddress_id => 972 )

=> [#<Zzperson id: 1347, ....>, #<Zzperson id: 1349, ...>, #<Zzperson id: 1968, ...>]

A lesson learned from long ago was to exit the rails console in between edits to the model files, which I continue to adhere to. Is there a way to inspect the model definition from within the console? I also recall the reload! command, but may date myself in suggesting that didn’t always work?

This is rails 3.2.11 (fresh install so everything should be current), as is activerecord at 3.2.11, etc. For what it’s worth, the PostgreSQL gem is pg (0.14.1). And PostgreSQL is at version 9.1.4.

And for completeness, the schema.rb was created by rake:schema:dump, and it looks valid to me:

create_table "zzaddress", :id => false, :force => true do |t|
   t.integer "id", :null => false
   <snip>
end

create_table "zzperson", :id => false, :force => true do |t|
   t.integer "id", :null => false
   t.integer "zzaddress_id", :default => 0, :null => false
   <snip>
end

The SQL statements file created by pg_dump shows creation of the tables as expected:

CREATE TABLE zzaddress (
   id integer NOT NULL,
   etc.
);

CREATE TABLE zzperson (
   id integer NOT NULL,
   zzaddress_id integer DEFAULT 0 NOT NULL,
   etc.
);
  • 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-17T07:23:00+00:00Added an answer on June 17, 2026 at 7:23 am

    Sigh. The answer is that I had been away from Rails for too long.

    aperson = Zzperson.where(:id=>123)
    

    returns a collection!

    aperson = Zzperson.where(:id=>123).first
    

    returns a person, which is what I had been looking for but forgetting all along.

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this

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.