麻烦的DataMapper连结一对一的模型 [英] Trouble linking one-to-one models in DataMapper

查看:166
本文介绍了麻烦的DataMapper连结一对一的模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用的PostgreSQL 的DataMapper 来建立一个辛纳屈的应用程序,运行到一些问题, DataMapper的(我是很新的它,使用轨道/ AR )。对于每个帐户创建的,它可以是一个艺术家地点风扇。只有一个,没有结合。

当用户注册,我可以创建帐户实例罚款,但我无法创建帐户的有1 艺术家地点风扇实例(为便于交谈,我们将使用艺术家作为属于模型)。

下面是我目前的模式声明:

 类帐户
    包括的DataMapper ::资源    属性:ID,序列
    ...    有1个,:艺术家:所需=>假
    有1个,:场地:需要=>假
    有1个,:风扇:要求=>假
结束级的艺术家
    包括的DataMapper ::资源    #没有ID
    ...    belongs_to的:帐户,:关键=>真正
结束一流的场馆
    包括的DataMapper ::资源    #没有ID
    ...    belongs_to的:帐户,:关键=>真正
结束范类
    包括的DataMapper ::资源    #没有ID
    ...    belongs_to的:帐户,:关键=>真正
结束

这就是我正在努力去注册过程:

 帐户= Account.create(
    ...
)account.Artist = Artist.create(:生物=>中考)

这是目前给我的下一个错误:

 未定义的方法`艺术家='#为LT&;帐号:0x007ff2af289840>


解决方案

我想你应该指模型中使用小写字母:

  account.artist = Artist.create(:生物=>中考)

这行:

 有1:艺术家:所需=>假

添加的关系,通过它可以从帐户参考艺术家

更新

这个答案有正是你的模型,并好像它的工作。有 DataMapper.finalize 的事情,我想你做到了。

Using PostgreSQL and DataMapper to build a Sinatra app, running into some problems with DataMapper (I'm very new to it, used to Rails/AR). For every Account created, it can either be an Artist, Venue, or Fan. Only one, no combining.

When the user signs up, I can create the Account instance fine, but I'm having trouble creating the account's has 1 Artist or Venue or Fan instance (for ease of conversation, we'll use Artist as the belonging model).

Here's my current model declarations:

class Account
    include DataMapper::Resource

    property :id,           Serial
    ...

    has 1, :artist, :required => false
    has 1, :venue, :required => false
    has 1, :fan, :required => false
end

class Artist
    include DataMapper::Resource

    # no ID
    ...

    belongs_to :account, :key => true
end

class Venue
    include DataMapper::Resource

    # no ID
    ...

    belongs_to :account, :key => true
end

class Fan
    include DataMapper::Resource

    # no ID
    ...

    belongs_to :account, :key => true
end

And here's how I'm trying to go about the sign up process:

account = Account.create(
    ...
)

account.Artist = Artist.create(:bio => "test")

This is currently giving me next error:

undefined method `Artist=' for #<Account:0x007ff2af289840>

解决方案

I guess you should use lowercase letter in referring model:

account.artist = Artist.create(:bio => "test")

This line:

has 1, :artist, :required => false

Add a relation, by which you can refer from Account to Artist.

Updated

This answer has exactly your model and seems like it worked. There is DataMapper.finalize thing, i guess you made it.

这篇关于麻烦的DataMapper连结一对一的模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆