Created
August 5, 2011 13:28
-
-
Save steverandy/1127539 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def set_position | |
if not self.special | |
categories = self.user.categories.normal.with_position.asc(:position) | |
if categories.present? && categories.count > 0 | |
self.position = categories.last.position + 1 | |
else | |
self.position = 1 | |
end | |
end | |
end |
I did not. I thought this is no longer necessary for Mongoid 2.x.
…On Aug 5, 2011, at 9:35 PM, paulelliott wrote:
Did you set inverse_of on the relation?
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1127539
You need it for the identity map to work correctly, so although it isn't "required" it is highly recommended.
…-- Paul
On Friday, August 5, 2011 at 9:42 AM, steverandy wrote:
I did not. I thought this is no longer necessary for Mongoid 2.x.
On Aug 5, 2011, at 9:35 PM, paulelliott wrote:
> Did you set inverse_of on the relation?
> ##
>
> Reply to this email directly or view it on GitHub:
> https://gist.github.com/1127539
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/1127539
Sorry it did actually have inverse_of.
class User
include Mongoid::Document
include Mongoid::Timestamps
embeds_many :categories, :validate => false
end
class Category
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :user, :inverse_of => :categories
end
The error was the following
NoMethodError: undefined method `categories' for nil:NilClass
The methods was called from self.user.categories.normal.with_position.asc(:position)
from the following codes.
def set_position
if not self.special
categories = self.user.categories.normal.with_position.asc(:position)
if categories.present? && categories.count > 0
self.position = categories.last.position + 1
else
self.position = 1
end
end
end
Where is the set_position method? Is it in the category?
Can you post the fabricators being used in here as well?
class Category
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :user, :inverse_of => :categories
before_create :set_position
def set_position
if not self.special
categories = self.user.categories.normal.with_position.asc(:position)
if categories.present? && categories.count > 0
self.position = categories.last.position + 1
else
self.position = 1
end
end
end
end
For the fabricator:
Fabricator(:category) do
title { Fabricate.sequence(:title) { |i| "Category #{i}" } }
end
Fabricator(:user) do
full_name { Fabricate.sequence(:full_name) { |i| "User #{i}" } }
username { Fabricate.sequence(:username) { |i| "user#{i}" } }
email { Fabricate.sequence(:email) { |i| "user#{i}@example.com" } }
password "password"
password_confirmation "password"
end
And the unit test
test "should have position" do
user = Fabricate :user
category = Fabricate :category, :user => user, :title => "Category"
assert_equal category.position, 1
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you set inverse_of on the relation?