Strange Datamapper behavior

Discussion in 'Ruby' started by rwz, May 24, 2009.

  1. rwz

    rwz Guest

    I've been trying to make a simple many-to-many associations.

    To get along with datamapper i copied the example from dm's homepage.
    Here we have few simple models for a blog.

    <code>require 'rubygems'
    require 'dm-core'
    require 'dm-validations'
    require 'dm-timestamps'

    DataMapper::Logger.new(STDERR, :debug)
    DataMapper.setup:)default, 'mysql://root@localhost/somedatabase')

    class Post
    include DataMapper::Resource

    # ...or the Serial custom-type which is functionally identical to
    the above:
    property :id, Serial, :key => true
    # ...or pass a :key option for a user-set key like the name of a
    user:
    property :name, String

    property :title, String
    property :body, Text
    property :created_at, DateTime
    end

    class Comment
    include DataMapper::Resource
    property :id, Serial

    property :posted_by, String
    property :email, String
    property :url, String
    property :body, Text
    end

    class Category
    include DataMapper::Resource
    property :id, Serial, :key => true
    property :name, String
    end


    class Post
    has n, :comments
    end

    class Comment
    belongs_to :post
    end


    class Categorization
    include DataMapper::Resource
    # property :id, Serial

    # property :created_at, DateTime

    belongs_to :category
    belongs_to :post
    end

    # Now we re-open our Post and Categories classes to define
    associations
    class Post
    has n, :categorizations
    has n, :categories, :through => :categorizations, :mutable => true
    end

    class Category
    has n, :categorizations
    has n, :posts, :through => :categorizations, :mutable => true
    end

    DataMapper.auto_migrate!
    </code>

    Then i try to do things with this models somehow:
    <code>
    post = Post.create:)title => 'first post', :body => 'first post body')
    post.save
    post.comments.build:)body => 'comment')
    post.save
    post.categories.build:)name => 'tag')
    post.save
    </code>

    and get ArgumentError exception: The property 'post_id' is not a
    public property.

    Exception goes from the line containing "post.categories.build:)name
    => 'tag')".

    I tried to dig in the code of datamapper to solve this, but it looks
    too complicated for me. :(

    Did anyone had this problem?
     
    rwz, May 24, 2009
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam Smoot

    ANN: DataMapper 0.2.3 Released

    Sam Smoot, Nov 5, 2007, in forum: Ruby
    Replies:
    1
    Views:
    94
    John Wells
    Nov 14, 2007
  2. Sam Smoot

    DataMapper 0.2.5 Release

    Sam Smoot, Dec 20, 2007, in forum: Ruby
    Replies:
    0
    Views:
    88
    Sam Smoot
    Dec 20, 2007
  3. Softmind Technology

    Comparing Active Record VS Datamapper...?

    Softmind Technology, Feb 2, 2008, in forum: Ruby
    Replies:
    11
    Views:
    478
    Sam Smoot
    Feb 20, 2008
  4. Old Echo
    Replies:
    1
    Views:
    142
    Old Echo
    Jul 18, 2008
  5. Bjoern
    Replies:
    0
    Views:
    113
    Bjoern
    Oct 4, 2009
Loading...

Share This Page