无法显示其他模型的信息

时间:2022-10-09 19:42:07

I did an application in rails and I'm trying to use a column type_id in relationship with my other table

我在rails中做了一个应用程序,我试图在与其他表的关系中使用列type_id

My tables:

我的桌子:

sinisters

sinisters

+---+----------+---------+
|id | client   | type_id |
+---+----------+---------+
| 1 | Charles  |       1 |
| 2 | Montalvo |       1 |
| 3 | Gabriel  |       2 |
| 4 | Miguel   |       2 |
+---+----------+---------+

sinister_types

sinister_types

+----+--------+   
| id | name   |
+----+--------+ 
|  1 | DANGER |
|  2 | FIRE   |
+----+--------+ 

My controller:

我的控制器:

@sinisters = Sinister.find(:all)

My models:

我的模特:

class Sinister < ActiveRecord::Base
   belongs_to :sinister_type
end

class SinisterType < ActiveRecord::Base
   has_many :sinisters
end

My view:

我的看法:

<% @sinisters.each |sinister| do %>
   <%= sinister.client %>
   <%= sinister.type.name %>
<% end %>

I want to show sinister types names.

我想展示险恶的类型名称。

I tried this code and got nothing:

我尝试了这段代码而一无所获:

<%= sinister.sinister_type.name %>

And also tried and got nothing

并且也尝试了什么都没有

<%= sinister.type.try(:name) %>

Please somebody can help me?

请有人可以帮帮我吗?

2 个解决方案

#1


2  

Like @backpackerhh says, the default convention is sinister_type_id, not type_id. But if you want to override it, you need to specify :foreign_key.

就像@backpackerhh所说,默认约定是sinister_type_id,而不是type_id。但是如果要覆盖它,则需要指定:foreign_key。

Model :

型号:

class Sinister < ActiveRecord::Base
  belongs_to :sinister_type, :foreign_key => :type_id
end

class SinisterType < ActiveRecord::Base
   has_many :sinisters
end


Controller :

控制器:

@sinisters = Sinister.find(:all)


View :

查看:

Not @sinisters.each |sinister| do, but @sinisters.each do |sinister|

不是@ sinisters.each |险恶的做,但@ sinisters.each做|险恶

<% @sinisters.each do |sinister| %>
   <%= sinister.client %> :
   <%= sinister.sinister_type.name %>
<% end %>

#2


1  

I think your column on sinister table should be sinister_type_id (instead of type_id) or you need to specify the foreign key on your model.

我认为你在阴险表上的列应该是sinister_type_id(而不是type_id),或者你需要在模型上指定外键。

#1


2  

Like @backpackerhh says, the default convention is sinister_type_id, not type_id. But if you want to override it, you need to specify :foreign_key.

就像@backpackerhh所说,默认约定是sinister_type_id,而不是type_id。但是如果要覆盖它,则需要指定:foreign_key。

Model :

型号:

class Sinister < ActiveRecord::Base
  belongs_to :sinister_type, :foreign_key => :type_id
end

class SinisterType < ActiveRecord::Base
   has_many :sinisters
end


Controller :

控制器:

@sinisters = Sinister.find(:all)


View :

查看:

Not @sinisters.each |sinister| do, but @sinisters.each do |sinister|

不是@ sinisters.each |险恶的做,但@ sinisters.each做|险恶

<% @sinisters.each do |sinister| %>
   <%= sinister.client %> :
   <%= sinister.sinister_type.name %>
<% end %>

#2


1  

I think your column on sinister table should be sinister_type_id (instead of type_id) or you need to specify the foreign key on your model.

我认为你在阴险表上的列应该是sinister_type_id(而不是type_id),或者你需要在模型上指定外键。