EMBEDDED DOCUMENT::help.....

29 views
Skip to first unread message

rahul chandra

unread,
Dec 4, 2011, 12:25:25 PM12/4/11
to MongoMapper
i have problem with this AS I AM NOT ABLE TO CALL PROFILE FROM USER IT
SHOWS UNDEFINED METHOD PROFILE.....

class User
include MongoMapper::Document
one :person, :class_name => 'Person', :foreign_key => :owner_id
end

class Person
include MongoMapper::Document
include ROXML

xml_accessor :_id
xml_accessor :email
xml_accessor :url
xml_accessor :profile, :as => Profile


key :email, String, :unique => true
key :url, String
key :owner_id, ObjectId

belongs_to :owner, :class_name => 'User'
one :profile, :class_name => 'Profile'
end

class Profile
include MongoMapper::EmbeddedDocument
include ROXML

xml_reader :person_id
xml_accessor :first_name
xml_accessor :last_name
xml_accessor :image_url


key :first_name, String
key :last_name, String
key :image_url, String
end

Jon Kern

unread,
Dec 4, 2011, 6:38:02 PM12/4/11
to mongo...@googlegroups.com
don't know if it will help you, but there is an embedded example here:
https://github.com/JonKernPA/mongo_examples
the Product -- Release mini sample...

jon

blog: http://technicaldebt.com
twitter: http://twitter.com/JonKernPA


rahul chandra said the following on 12/4/11 12:25 PM:

Brian Hempel

unread,
Dec 4, 2011, 8:01:54 PM12/4/11
to mongo...@googlegroups.com
As your models are written now, you'd have to do...

my_user.person.profile

If you want to abbreviate that, you can define a profile method on User...

def profile
person.profile
end

Brian

> --
> You received this message because you are subscribed to the Google
> Groups "MongoMapper" group.
> For more options, visit this group at
> http://groups.google.com/group/mongomapper?hl=en?hl=en

rahul chandra

unread,
Dec 5, 2011, 12:19:10 AM12/5/11
to mongo...@googlegroups.com
@ >Brian

i defined the profile method still same error i tried it in console also.
can u modify the models so that it can work properly....please.

Brian Hempel

unread,
Dec 5, 2011, 12:31:14 AM12/5/11
to mongo...@googlegroups.com
I threw your classes into the MongoMapper console (mmconsole command) and it worked fine with the profile method I described.


my_user = User.create(:person => Person.new(:profile => Profile.new))
my_user.profile # => #<Profile _id: BSON::ObjectId('4edc555578fca28259000001')>

Brian

rahul chandra

unread,
Dec 5, 2011, 1:48:44 AM12/5/11
to mongo...@googlegroups.com
yes in console it is working but not in browser is there anything i can do with the controllers,
the error its giving me is

NoMethodError in Dashboards#index

Showing /home/rahul/Rails_projects/staytuned/app/views/dashboards/index.html.haml where line #3 raised:

undefined method `profile' for nil:NilClass

Extracted source (around line #3):

1: - if user_signed_in?
2:   %h1#user_name
3:     = link_to current_user.real_name, root_path
4:     /%span= title_for_page
5:   %h3#latest_message
6:     = my_latest_message

rahul chandra

unread,
Dec 5, 2011, 1:53:46 AM12/5/11
to mongo...@googlegroups.com
this is the thing that is giving me error

User class-->

  def real_name
    "#{person.profile.first_name.to_s} #{person.profile.last_name.to_s}"
  end

and i changed it to

  def real_name
    "#{self.first_name.to_s} #{self.last_name.to_s}"
  end

then it ll work but i need first and last names of profile...

Brian Hempel

unread,
Dec 5, 2011, 2:31:06 AM12/5/11
to mongo...@googlegroups.com
Your current_user does not have a person.  Watch out, MongoMapper associations only talk to each other through the database.  If current_user is saved in an instance variable (say, @current_user) and afterwards, separately, you load up a user, add a person, and save it, then the @current_user object may not have the person until you do @current_user.reload.

If that's not the problem, then there's probably a logic error in your application.

Brian

rahul chandra

unread,
Dec 5, 2011, 6:38:20 AM12/5/11
to mongo...@googlegroups.com
ok tq.
Reply all
Reply to author
Forward
0 new messages