class Board < ActiveRecord::Base
has_many :applications, dependent: :destroy
end
class Application < ActiveRecord::Base
belongs_to :board
validates_presence_of :board
has_many :interactions, dependent: :destroy
end
class Interaction < ActiveRecord::Base
belongs_to :application
validates_presence_of :application
end
Given the above ActiveRecords, in the show method of boards_controller, I can call @boards.applications, and even though I don't explicitly call application.interactions I still have access to the interactions in the view. However, for this particular view, I only need one interaction, which is gathered through some logic having to do with nil checks and sorting.
I would rather do this logic in the controller and only pass that one interaction in instead of all the extras for every application, but currently it's passing all of them and I can't explicitly add an application.current_interaction in the controller because it's an unknown attribute.
How can I set one interaction for each application, and what is the proper way to do it in Ruby on Rails?
Here's what I ended up doing:
The application model should look like this:
class Application < ActiveRecord::Base
belongs_to :board
validates_presence_of :board
has_many :interactions, dependent: :destroy
def current_interaction
#logic here
return interaction
end
end
Then it can be called in the view with <%= application.current_interaction %>, there shouldn't have to be any changes to the controller at all.
current_interactionto yourApplicationmodel.