Levi Melvin
Levi Melvin

Reputation: 11

Ruby on Rails Index Action, Display Records

Sorry for the ignorant question, I'm new to Ruby on Rails.

I have 3 models - Employer, Employee and Announcement

The Announcement model belongs_to the Employer The Employer has_many Announcements The Employee belongs_to the Employer

Currently I'm able to display the Announcements that belong_to an Employer using

@announcements = Announcement.where(:employer_id => current_employer.id)

I'm having trouble display the Announcements that belong to an Employer to the Employee.

What query would I use to do this

Upvotes: 1

Views: 93

Answers (5)

Richard Peck
Richard Peck

Reputation: 76784

Associations

Firstly, you should not be calling a .where query on your associative data:

@announcements = Announcement.where(:employer_id => current_employer.id)

Since you're new to RoR, I'd recommend having a quick read of the ActiveRecord Association documentation to give you a much richer understanding of how the associations work.

Specifically, you want to make sure that every time you have associated data, you should be able to call it from your "parent" object. ActiveRecord will take your associations & automatically load the required data (with the help of foreign_keys):

@announcements = current_employer.accouncements

--

Fix

In order to get it working, you'll want to do this:

#app/models/employer.rb
class Employer < ActiveRecord::Base
   has_many :employees
   has_many :announcements
end

#app/models/employee.rb
class Employee < ActiveRecord::Base
   belongs_to :employer
   delegate :announcements, to: :employer
end

#app/models/announcement.rb
class Announcement < ActiveRecord::Base
   belongs_to :employer
end

This will give you the ability to call the following:

@employee = current_employer.employees.find 1
@announcements = @employee.announcements

Upvotes: 0

Nitin
Nitin

Reputation: 7366

As per your your question requirement. You are not having proper association. You should have following association.

employer has_many announcements

Class Employer
  has_many :announcements
  has_many :employees, through :announcements
end

Class Employee
  belongs_to :announcement
end

Class Announcement
  belongs_to :employer, through :announcements
  belongs_to :announcement
end

Now find it like this :

@announcements = current_employer.announcements

And employees like this :

@announcements.each do |announcement|
  @employees = announcement.employees
end

Upvotes: 0

Abdul Baig
Abdul Baig

Reputation: 3721

If you want to frequently use this Employee and Announcement relationship then make a new relation of has_many through like:

Class Employee
  has_one :employer
  has_many :announcements, through: :employer
end

Then you can directly have announcements belonging to the employee by simply doing:

@employee.announcements

And for deep understanding you can refer to 2.4 The has_many :through Association

Upvotes: 1

MoskeyOmbus
MoskeyOmbus

Reputation: 201

You should be able to display all of the Employer's announcements using

current_employer.announcements

Assuming the Employer has_many Employees, given a single @employee

@employee.employer.announcements

Upvotes: 0

Tom Kadwill
Tom Kadwill

Reputation: 1478

Given the information that you have provided I think you want to Display the announcements for a specific employees, employer. Eg:

Employee -> Employer -> Announcement

In this case you could do something like:

@employeer = Employee.employeer #assuming this is a one-to-one relationship
@announcements = Announcement.where(:employer_id => @employeer.id)

Upvotes: 0

Related Questions