Created
July 29, 2010 18:39
-
-
Save iamnader/498876 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
### Models ### | |
class Post | |
include Mongoid::Document | |
references_many :properties, :stored_as => :array, :inverse_of => :posts, :index => true | |
end | |
class Property | |
include Mongoid::Document | |
field :name | |
references_many :posts, :stored_as => :array, :inverse_of => :properties | |
end | |
### posts/_form.html.haml ### | |
- form_for @post do |f| | |
= select_tag 'post[property_ids][]', options_from_collection_for_select(Property.find(:all), :id, :name, @post.properties.map {|p| p.id}), {:size => 3, :multiple => true} | |
%p | |
= f.submit | |
### Post Controller ### | |
def create | |
@post = Post.find(params[:id]) | |
if @post.save | |
... | |
######## | |
# This sumbits a post that looks like this | |
######## | |
Started POST "/posts" for 127.0.0.1 at Thu Jul 29 08:49:07 -0700 2010 | |
Processing by PostsController#create as HTML | |
Parameters: {"commit"=>"Create Post", "post"=>{"property_ids"=>["4c5078608035442eb6000003"]}, "authenticity_token"=>"m44qVAUkzc7d0p1OT5JzlLJVtwdLeLmdIqB6nTkFjFQ=", "_snowman"=>"☃"} | |
MONGODB dev['properties'].find({:_id=>{"$in"=>["4c5078608035442eb6000003"]}}, {}) | |
######## | |
# and then if we look at what was created we can see that the post was saved | |
# with a property_ids array of strings instead of objectids and the | |
# properties association doesn't work | |
######## | |
$ rails c | |
>> post = Post.find(:last) | |
=> <Post _id: 4c51a2f48035442f7400000d, property_ids: ["4c5078608035442eb6000003"], user_id: BSON::ObjectID('4c5078608035442eb6000001')> | |
>> post.properties.to_json | |
=> "[]" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment