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
has_many :connections
has_many :vacancies, through: :connections
connection.rb
belongs_to :vacancy
belongs_to :resume
Resumes and Vacancies tables has id, name columns
Connections table has id , resume_id , vacancy_id columns
My code - @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 -
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? or do you want to collect
the name of each vacancy from the set of collections?
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.