与has_many:通过关联的奇怪关联错误

时间:2012-04-05 09:42:33

标签: ruby-on-rails ruby-on-rails-3

////////更新

当我将表单作为更新提交时,这是开发日志的输出

  

LogoCategory加载(0.3ms)SELECT logo_categories。* FROM   logo_categories内部加入logos logo_categoriesid =   logoslogo_category_id内部加入logos_postslogosid =   logos_postslogo_id WHERE logos_postspost_id = 61

//////////////

如果你有一个类似下面的应用程序设置,有没有人知道他们为什么会收到这样的错误...我无法在任何地方找到任何信息。

PostsController中的

ActiveRecord :: HasManyThroughNestedAssociationsAreReadon#update

  

无法修改关联'Post#logo_categories',因为它会去   通过一个以上的其他协会。

post.rb

has_and_belongs_to_many :logos
has_many :logo_categories, :through => :logos  

logo.rb

  belongs_to :logo_category
  has_and_belongs_to_many :posts

logo_category.rb

has_many :logos
has_and_belongs_to_many :posts

我的桌子是......

  

帖子(id),徽标(id),logos_posts(id,logo_id,logo_category_id),   logo_categories(id)

当我在编辑帖子记录时尝试保存帖子模型的_form并且我选中或取消选中logo_category复选框时,我收到错误。

欢迎任何想法!感谢

1 个答案:

答案 0 :(得分:1)

尝试:

post.rb

has_and_belongs_to_many :logos

logo.rb

has_and_belongs_to_many :posts

已编辑

您的'logo_categories'迁移如下:

  create_table :logo_categories do |t|
    t.references :logo
    t.references :post
    t.timestamps
  end

看看链接,它将帮助您使用HABTM:

http://asciicasts.com/episodes/17-habtm-checkboxes

http://ramblings.gibberishcode.net/archives/rails-has-and-belongs-to-many-habtm-demystified/17

相关问题