【问题标题】:Rails Habtm with a field带有字段的 Rails Habtm
【发布时间】:2010-05-31 15:18:41
【问题描述】:

拥有和属于许多表中是否可以有一个字段?

就像最喜欢的一样:

create_table :messages_users, :id => false, :force => true do |t|
  t.integer :message_id, :null => false
  t.integer :user_id, :null => false

  t.boolean :favorite, :null => false

  t.timestamps
end

感谢 ActiveRecord,我看到时间戳运行良好。但是当我尝试将收藏夹添加到表格中然后我尝试:

Message.first.users << User.first

然后我收到此错误消息:

ActiveRecord::StatementInvalid: SQLite3::SQLException: messages_users.favorite 可能不是 NULL:插入“messages_users” ("created_at", "message_id", "updated_at", "user_id") 值 ('''2010-05-27 06:07 :50.721512''', 1, '''2010-05-27 06:07:50.721512''', 1)

我想使用一个 habtm,但我不喜欢 has_many foo 虽然酒吧协会:)

这可能吗?

非常感谢。

【问题讨论】:

    标签: ruby-on-rails activerecord has-and-belongs-to-many


    【解决方案1】:

    不,这是不可能的。
    尽管 bar 使用 has_many foo。

    【讨论】:

    • 如果你想比较 habtm 和 has_many :through 这是[正确的起点][1]。该博客还有许多关于 has_many 的其他文章:我鼓励您深入研究这些文章。基本上 clyfe 是对的,我不明白为什么你更喜欢 habtm,因为它不是在你试图达到的范围内创建的。切换到 has_many :through,一旦你了解它的工作原理,你会惊讶于它的灵活性。 [1]:blog.hasmanythrough.com/2006/2/28/association-goodness
    • 感谢您的回答。我想我会使用 has_many :through :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    相关资源
    最近更新 更多