Help with #Rails relationships

Asked

Viewed 40 times

1

I have 3 tables

area
name

area_shared
area_id
request_id

request
area_shared_id
area_id

I would like to display my table this way <%[email protected]_shared.area_name%>

but I think my relationships are wrong, someone can help me?

Note: Remembering that inside the form request I have a fields_for with area_shared that works, the problem is only to display even.

  • How’s your relationship these days? You can show the code?

2 answers

0

To be able to call @request.area_shared you need a relation 1(request) to 1(area_shared). Same as area_shared for area if that’s what you want. (@request.area_shared.area.name)

area name #belongs_to :area_shared area_shared_id

area_shared request_id #belongs_to :request #has_one :area

request #has_one :area_shared

0

I would do so:

class Area < ...
    has_many :shared_areas
end

and...

class Request < ...
    belongs_to :shared_areas
end

and...

class SharedArea < ...
    belongs_to :area
    has_many :requests
end

With that, I could do:

<%= @request.shared_area.area.name %>

Anyway, show me your code so I can improve my answer. But from what I understand, that’s what you need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.