简体   繁体   English

Ruby on Rails,从多个模型实例获取属性

[英]Ruby on Rails, getting attributes from multiple model instances

In my Ruby on Rails project, I have a Country model class and a Proposal model class. 在我的Ruby on Rails项目中,我有一个Country模型类和一个Proposal模型类。 A country has many proposals and a proposal belongs to a specific country. 一个国家有很多建议,而一个建议属于一个特定的国家。 Each proposal consists of 30 questions. 每个提案包含30个问题。 I want to display each question for every proposal for that specific country. 我想针对该特定国家/地区的每个提案显示每个问题。

My initial idea for syntax was: 我最初的语法想法是:

<% @country.proposal.each do |proposal| %>

<% end %>

However, this resulted in an error message 但是,这导致出现错误信息

undefined method `proposal' for #Country:0x007f67b51cf178

Any ideas? 有任何想法吗?

According to yous associations, just replace @country.proposal.each with @country.proposals.each . 根据您的关联,只需将@country.proposal.each替换为@country.proposals.each has_many association define method with a name in plural form. has_many关联定义方法,其名称具有复数形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM