Reputation: 103
I need help in doing the following join using ActiveRecord relation models:
select "access_urls"."id", "controller_urls"."controller", "action_urls"."action" from
"access_urls"
inner join "controller_urls"
on "controller_urls"."id" = "access_urls"."controller_url_id"
inner join "action_urls"
on "action_urls"."id" = "access_urls"."action_url_id"
Where I have AccessUrl, ActionUrl and ControllerUrl models
class AccessUrl < ActiveRecord::Base
belongs_to :controller_url
belongs_to :action_url
end
class ActionUrl < ActiveRecord::Base
has_many :access_urls
validates :action, presence: true, uniqueness: { message: "já encontra-se em uso." }
end
class ControllerUrl < ActiveRecord::Base
has_many :access_urls
validates :controller, presence: true, uniqueness: { message: "já encontra-se em uso." }
end
Can anyone help me?
Upvotes: 0
Views: 32
Reputation: 38645
Using joins
:
result = AccessUrl.joins(
:controller_url, :action_url
).select(
'access_urls.id, controller_urls.control, action_urls.action'
)
This will give you a relation result
containing objects of AccessUrl
. You can loop through the result
and access the selected columns as:
result.each do |r|
# r.id
# r.control
# r.action
end
Upvotes: 1