저는 MongoDB 및 MongoID의 실제 초보자입니다. 나는 내가 Objet
새로운 만들 때 무엇을 얻을 두 개의 발판MongoID가 포함 된 문서의 한 필드 만 표시
class Objet
include Mongoid::Document
field :nom, type: String
embeds_one :coordonnee
end
그리고
class Coordonnee
include Mongoid::Document
field :adresse1, type: String
field :adresse2, type: String
field :code_postal, type: String
field :ville, type: String
embedded_in :objet
end
을 만들어 : 이제
를, 나는 단지 현장을 보여주기 위해 노력하고있어 이 문서는 adresse1
이지만 작동하지 않습니다. 나는이 일을 단지 전체 포함 된 문서를 표시 할 수 있습니다
내가 할 경우 :
<%= @objet.coordonnees.adresse1 %>
내가이 오류 :
undefined method `adresse1' for #<Hash:0x2b2b1f0>
내가 어떻게 할 수 있습니까?
편집
, 나는 "Adresse1, adresse2, 빌, code_postal"모든 요소를 표시 할 수 지내요 :
Controller
def show
@objet = Objet.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @objet }
end
end
View
<%= @objet.nom %>
<% @objet.coordonnee.each do |t| %>
<%= t[1] %>
<% end %>
하지만 내 질문은 : 어떻게 그 중 하나를 표시하려면? 빌과 같은, 또는 code_postal 또는 adresse1 ...?
내 질문에 관심을 가져 주셔서 감사합니다. show.html.erb에서 어떻게이 db.collection.find ({}, { "adresse1": 1})을 변환 할 수 있습니까? –
@ GrégoireC 현재 show.html.erb를 게시 할 수 있습니까? –
내 게시물을 편집합니다 :) –