Skip to content

Instantly share code, notes, and snippets.

@ceritium
Created September 26, 2011 17:03
Show Gist options
  • Save ceritium/1242744 to your computer and use it in GitHub Desktop.
Save ceritium/1242744 to your computer and use it in GitHub Desktop.
class Project
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :histories
has_many :invitations
field :name, type: String
validates_presence_of :name
has_and_belongs_to_many :users
end
# work
def create
@project = Project.new(params[:project])
if @project.save
@project.users << current_user
redirect_to project_path(@project), :notice => 'Project created'
else
render 'new'
end
end
# no work
def create
@project = current_user.projects.build(params[:project])
if @project.save
redirect_to project_path(@project), :notice => 'Project created'
else
render 'new'
end
end
class User
include Mongoid::Document
# Include default devise modules. Others available are:
# :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
field :name
validates_presence_of :name
validates_uniqueness_of :name, :email, :case_sensitive => false
attr_accessible :name, :email, :password, :password_confirmation, :remember_me
has_and_belongs_to_many :projects
end
@Nerian
Copy link

Nerian commented Sep 26, 2011

Mmm, it seems that project is not being marked as dirty and so is not being saved.

What happen if you save from the User side?

ruby-1.9.2-p290 :015 > u = User.last
ruby-1.9.2-p290 :016 > u.projects
ruby-1.9.2-p290 :017 > p = u.projects.build(:name => 'test')
ruby-1.9.2-p290 :018 > u.save

@ceritium
Copy link
Author

ruby-1.9.2-p290 :049 > u = User.last
 => #<User _id: 4e80aeb894fed428d7000006, _type: nil, email: "[email protected]", encrypted_password: "$2a$10$x293r.HK19do3f2H113nT.vdFZcrHRPvvPL6p5cBXoBzkMNasJB4O", remember_created_at: nil, reset_password_token: nil, reset_password_sent_at: nil, sign_in_count: 1, current_sign_in_at: 2011-09-26 16:56:24 UTC, last_sign_in_at: 2011-09-26 16:56:24 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "joselito", project_ids: []> 
ruby-1.9.2-p290 :050 > u.projects
 => [] 
ruby-1.9.2-p290 :051 > p = u.projects.build(:name => 'test')
 => #<Project _id: 4e80dbd594fed4360f000005, _type: nil, created_at: nil, updated_at: nil, user_ids: [BSON::ObjectId('4e80aeb894fed428d7000006')], name: "test"> 
ruby-1.9.2-p290 :052 > u.save
 => true 
ruby-1.9.2-p290 :053 > 
ruby-1.9.2-p290 :054 >   
ruby-1.9.2-p290 :055 >   
ruby-1.9.2-p290 :056 >   u
 => #<User _id: 4e80aeb894fed428d7000006, _type: nil, email: "[email protected]", encrypted_password: "$2a$10$x293r.HK19do3f2H113nT.vdFZcrHRPvvPL6p5cBXoBzkMNasJB4O", remember_created_at: nil, reset_password_token: nil, reset_password_sent_at: nil, sign_in_count: 1, current_sign_in_at: 2011-09-26 16:56:24 UTC, last_sign_in_at: 2011-09-26 16:56:24 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "joselito", project_ids: [BSON::ObjectId('4e80dbd594fed4360f000005')]> 
ruby-1.9.2-p290 :057 > u.reload
 => #<User _id: 4e80aeb894fed428d7000006, _type: nil, email: "[email protected]", encrypted_password: "$2a$10$x293r.HK19do3f2H113nT.vdFZcrHRPvvPL6p5cBXoBzkMNasJB4O", remember_created_at: nil, reset_password_token: nil, reset_password_sent_at: nil, sign_in_count: 1, current_sign_in_at: 2011-09-26 16:56:24 UTC, last_sign_in_at: 2011-09-26 16:56:24 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "joselito", project_ids: [BSON::ObjectId('4e80dbd594fed4360f000005')]> 
ruby-1.9.2-p290 :058 > u
 => #<User _id: 4e80aeb894fed428d7000006, _type: nil, email: "[email protected]", encrypted_password: "$2a$10$x293r.HK19do3f2H113nT.vdFZcrHRPvvPL6p5cBXoBzkMNasJB4O", remember_created_at: nil, reset_password_token: nil, reset_password_sent_at: nil, sign_in_count: 1, current_sign_in_at: 2011-09-26 16:56:24 UTC, last_sign_in_at: 2011-09-26 16:56:24 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "joselito", project_ids: [BSON::ObjectId('4e80dbd594fed4360f000005')]> 
ruby-1.9.2-p290 :059 > u.projects
 => [] 
ruby-1.9.2-p290 :060 > p
 => #<Project _id: 4e80dbd594fed4360f000005, _type: nil, created_at: nil, updated_at: nil, user_ids: [BSON::ObjectId('4e80aeb894fed428d7000006')], name: "test"> 
ruby-1.9.2-p290 :062 > p.users
 => [#<User _id: 4e80aeb894fed428d7000006, _type: nil, email: "[email protected]", encrypted_password: "$2a$10$x293r.HK19do3f2H113nT.vdFZcrHRPvvPL6p5cBXoBzkMNasJB4O", remember_created_at: nil, reset_password_token: nil, reset_password_sent_at: nil, sign_in_count: 1, current_sign_in_at: 2011-09-26 16:56:24 UTC, last_sign_in_at: 2011-09-26 16:56:24 UTC, current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", name: "joselito", project_ids: [BSON::ObjectId('4e80dbd594fed4360f000005')]>] 
ruby-1.9.2-p290 :063 > p.reload
Mongoid::Errors::DocumentNotFound: No se encontró ningún documento para la clase Project con los id(s) 4e80dbd594fed4360f000005.
    from /Users/ceritium/.rvm/gems/ruby-1.9.2-p290@scrum/gems/mongoid-2.2.1/lib/mongoid/document.rb:149:in `reload'
    from (irb):63
    from /Users/ceritium/.rvm/gems/ruby-1.9.2-p290@scrum/gems/railties-3.1.0/lib/rails/commands/console.rb:45:in `start'
    from /Users/ceritium/.rvm/gems/ruby-1.9.2-p290@scrum/gems/railties-3.1.0/lib/rails/commands/console.rb:8:in `start'
    from /Users/ceritium/.rvm/gems/ruby-1.9.2-p290@scrum/gems/railties-3.1.0/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

@Nerian
Copy link

Nerian commented Sep 26, 2011

Exactly, project is not being marked as dirtyand thus is not saved automatically. As to why, I don't know :)

This should work:

def create
    @project = current_user.projects.new(params[:project])
    if @project.save && current_user.save
      redirect_to project_path(@project), :notice => 'Project created'
    else
      render 'new'
    end

@werelax
Copy link

werelax commented Sep 26, 2011

@Nerian But if for some reason @project.save works but current_user.save blows up, you will end with a corrupted relationship. I think that is safer to go with something like

def create
  @project = current_user.create!(params[:project])
  redirect_to @project, :notice => 'Project created'
rescue Mongoid::Errors::Validations
  # You can check for sanity here
  render 'new'
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment