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.
);
4

2 回答 2

0

如果我没记错的话,我相信set_table_name已被弃用。尝试

self.table_name = 'zzaddress'

self.table_name = 'zzperson'
于 2013-01-14T05:06:38.067 回答
0

叹。答案是我离开 Rails 太久了。

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

返回一个集合!

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

返回一个人,这是我一直在寻找但一直忘记的。

于 2013-01-14T10:04:52.723 回答