使用连接模型(has_many 至)和额外字段创建和更新两个模型

发布于 2024-10-19 10:57:03 字数 448 浏览 0 评论 0原文

我一直在尝试简化我上一篇文章的上下文(大约 6 小时前:https://stackoverflow.com/questions/5150031/linking-third-models-through-two-join-models),只需通过连接链接我的两个主要列(属性和用户)模型(演员),其中我有两个 id(property_id 和 user_id)和一个字符串列(角色),我将为其添加纯文本。但是,我什至不知道如何在视图中为这个简单的模型进行简单的创建;我已经阅读了大约几十个关于 has_many :through 和额外字段的线程,但仍然不知道如何做到这一点...有人对如何实现这个有一个简单的答案吗?看来如果我使用.net,我大约两周前就完成了......

I've been trying to simplify the context of a previous post of mine (from about 6 hours ago: https://stackoverflow.com/questions/5150031/linking-three-models-through-two-join-models) by simply having my two main columns (properties and users) linked through a join model (actorships) in which I have the two ids (property_id and user_id) and a string column (role) for which I'd add plain text. However, I don't even know how to do a simle create in the view for this simple model; I've read like a couple dozen threads about the has_many :through and the extra fields and still have no clue how to do it... Anyone has a simple answer to how to implement this? It seems that if I was using .net, I would have finished about 2 weeks ago...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

已下线请稍等 2024-10-26 10:57:03
class User < ActiveRecord::Base
  has_many :properties, :through => :actorships
  has_many :actorships
end

class Property < ActiveRecord::Base
  has_many :users, :through => :actorships
  has_many :actorships
end

class Actorship < ActiveRecord::Base
  belongs_to :user
  belongs_to :property
end

user = User.last
new_actorship = user.actorships.create(:property=>Property.last, :role => 'omg')
# => Actorship associated to User and Property, with role 'omg'

user.properties
# => [Property.last]
class User < ActiveRecord::Base
  has_many :properties, :through => :actorships
  has_many :actorships
end

class Property < ActiveRecord::Base
  has_many :users, :through => :actorships
  has_many :actorships
end

class Actorship < ActiveRecord::Base
  belongs_to :user
  belongs_to :property
end

user = User.last
new_actorship = user.actorships.create(:property=>Property.last, :role => 'omg')
# => Actorship associated to User and Property, with role 'omg'

user.properties
# => [Property.last]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文