Help with relationships #rails

1

I have 3 tables

  

area
name

     

area_shared
area_id
request_id

     

request
area_shared_id
area_id

I'd like to display my table in this way <% = @ request.area_shared.area_name% >

But I think my relationships are wrong, can anyone help me?

Obs: Remembering that within the request form I have a fields_for with area_shared that works, the problem is only to display itself.

    
asked by anonymous 08.01.2016 / 13:25

2 answers

0

In order to call @ request.area_shared you need a relation 1 (request) to 1 (area_shared). The same from area_shared to 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

    
19.01.2016 / 14:33
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 this, I could do:

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

In any case, show me how your code is so I can improve my answer. But from what I understand, that's what you need.

    
19.01.2016 / 14:53