A已加入多个数据库,如图所示

class Address < ActiveRecord::Base
  has_many :years, dependent: :destroy
  has_many :people, through: :years
end

class Year < ActiveRecord::Base
  belongs_to :address
  belongs_to :person
end

class Person < ActiveRecord::Base
  has_many :years, dependent: :destroy
  has_many :addresses, through: :years
end

多年来,我在列表视图上遇到了问题,即Views>Years>list.html.erb
下面是一个不起作用的示例:
# table headers here
<% @years.each do |y| %>
   <tr>
     <td><%= y.year_date %></td>
     <td><%= y.person_id %></td>
     <td><%= y.person.given_name %></td>
     <td><%= y.person.last_name %></td>
    ...
<% end %>

year_date是指如果given_namelast_name行不存在,则此人在地址处显示“what”并显示“fine”的日期。
而且year.person_id显示也很好。
但不显示year.person.given_nameyear.person.last_namegiven_namelast_name是Person表中的字段。
错误是undefined method given_name for nil:NilClass
显然有点不对劲或者这会更明显。问题的一部分可能在于如何表达搜索。
此处填写代码:https://bitbucket.org/MtnBiker/crores5
谢谢你的建议。找了好几天了。
编辑:环顾四周开始怀疑外键,以下是我通过pgAdmin在数据库中找到的。
ALTER TABLE people
  ADD CONSTRAINT fk_rails_56c79562d9 FOREIGN KEY (address_id)
      REFERENCES addresses (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION;

编辑2。添加控制器:
class YearsController < ApplicationController
  helper_method :sort_column, :sort_direction
  before_action :set_year, only: [:show, :edit, :update, :destroy]

  def index
    @years = Year.all
    @years = Year.order(sort_column + " " + sort_direction)
  end

架构?
CREATE TABLE years
(
  id serial NOT NULL,
  year_date date,
  created_at timestamp without time zone NOT NULL,
  updated_at timestamp without time zone NOT NULL,
  resto boolean,
  resid boolean,
  source text,
  person_id integer,
  address_id integer,
  title character varying,
  CONSTRAINT years_pkey PRIMARY KEY (id),
  CONSTRAINT fk_rails_8fc1813509 FOREIGN KEY (person_id)
      REFERENCES people (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_rails_e1624dbb3f FOREIGN KEY (address_id)
      REFERENCES addresses (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE years
  OWNER TO gscar;

-- Index: index_years_on_address_id

-- DROP INDEX index_years_on_address_id;

CREATE INDEX index_years_on_address_id
  ON years
  USING btree
  (address_id);

-- Index: index_years_on_person_id

-- DROP INDEX index_years_on_person_id;

CREATE INDEX index_years_on_person_id
  ON years
  USING btree
  (person_id);

编辑:在这个漫长(抱歉)的讨论结束时给出答案。

最佳答案

undefined method given_name for nil:NilClass

这意味着您正在对未定义的变量调用given_name。在您的情况下,person.given_name
问题是您没有关联的person对象(即它不存在),或者关联没有正确设置。
--
调试的几个步骤:
是否已填充person_id
架构设置是否正确?
最可能的问题是您的person_idforeign key)值不存在。通过使用rails console来测试是否存在该值,您将能够看到这一点:
$ rails c
$ years = Year.all
$ years.each do |year|
$ - year.person_id
$ end
$ -> output of person_id values

如果您没有person_id的值,则意味着您的Year无法与相应的Person关联(因此出现问题)。要修复它,您需要确保数据库中存在respective foreign keys & objects
#app/models/year.rb
class Year < ActiveRecord::Base
   belongs_to :address
   belongs_to :person

   validates :person, :address, presence: true
end

-
开始想外键
这似乎不是问题(我检查了您的迁移):
ruby-on-rails - 联接表的 ListView-LMLPHP
上面显示了在您的has_many :through模型中,address_id所需的外键应该是person_id&Year。表中不需要foreign_key,除非Model具有belongs_to
更新
解决方法是使用.try
#app/views/years/index.html.erb
...
  <%= y.person.try(:given_name) %>

关于ruby-on-rails - 联接表的 ListView ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35108784/

10-15 03:19