Dom Barker
Dom Barker

Reputation: 1918

Parent fields are all nil when accessed by child (embedded 1-n)

I have a 1-n relationship defined as follows:

class User
  field :email, type: String
  embeds_many :papers
end

class Paper
  embedded_in :user
end

If I try and access the parent fields (user) from the child (paper) like so:

User.all.map(:papers).flatten.first.user.email

Then I get nil :(

Accessing like this works fine though:

User.all.first.papers.first.user.email

Upvotes: 1

Views: 89

Answers (1)

shingara
shingara

Reputation: 46914

It's a mispelling lire report on comment. To call a method on a map,you need use & before your symbol.

Try:

User.all.map(&:papers).flatten.first.user.email

Upvotes: 1

Related Questions