[英]Ruby on Rails Active Record through
I've been trying got data from another table and it doesn't work. 我一直在尝试从另一个表中获取数据,但它不起作用。 I have three tables: vacancies , resumes , connections . 我有三个表: 空缺 , 简历 ,人脉 。
vacancy.rb 空缺
has_many :connections
has_many :resumes, through: :connections
resume.rb resume.rb
has_many :connections
has_many :vacancies, through: :connections
connection.rb connection.rb
belongs_to :vacancy
belongs_to :resume
Resumes and Vacancies tables has id, name columns 简历和空缺表具有ID,名称列
Connections table has id , resume_id , vacancy_id columns 连接表具有id , resume_id和vacancy_id列
My code - @candidates = Resume.joins(:connections).where(connections: { vacancy_id: current_user.vacancies })
我的代码- @candidates = Resume.joins(:connections).where(connections: { vacancy_id: current_user.vacancies })
It's showing data from Resume table, but I can't fetch it with Vacancies table. 它显示的是“恢复”表中的数据,但我无法使用“空缺”表来获取数据。 How can I output vacancy name for example? 例如,如何输出空缺名称? I tried resume.connections.vacancy.name
in view but doesn't work - 我在视图中尝试过resume.connections.vacancy.name
,但不起作用-
undefined method `vacancy' for #<Connection::ActiveRecord_Associations_CollectionProxy:0xb43ac9d8>
Ah - yes, that's just not going to work... you are calling "vacancy" on a set of connections... (arrays don't have a method called "vacancy" but each member of that array will). 嗯-是的,那只是行不通的……您在一组连接上调用“空缺”……(数组没有称为“空缺”的方法,但是该数组的每个成员都可以)。 So: do you want to call "vacancy" on each
connection? 那么:您是否想在each
连接上each
称为“空缺”? or do you want to collect
the name of each vacancy from the set of collections? 或者你想collect
从集合集合每个空缺职位的名称?
try something like this instead: 尝试这样的事情:
vacancy_names = resume.connections.map{|con| con.vacancy.name }
alternatively: 或者:
resume.connections.each do |con|
puts con.vacancy.name
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.