关联has_many Rails模型

时间:2013-04-11 11:38:26

标签: ruby-on-rails ruby-on-rails-3.2 model-associations

我有三个模型,用户,帖子和书,通过第四个,kms相关联。

  • 邮政模型有很多公里和书籍通过
  • 书籍模型有很多公里,并通过
  • 发布
  • km属于book and post

我已针对上述案例尝试this tutorial并成功。

现在,我想创建一个说明帖子的协会。

  • 用户has_many kms
  • Km属于用户

社团:

class User < ActiveRecord::Base
  include Tenantable::Schema::Model
  has_many :kms, dependent: :destroy

  devise :database_authenticatable, :registerable,
  :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :email, :password, :password_confirmation, :remember_me


end

class Post < ActiveRecord::Base
  attr_accessible :name, :year, :author, :book_ids
  has_many :kms
  has_many :books, through: :kms   
end

class Book < ActiveRecord::Base
  attr_accessible :name
  has_many :kms
  has_many :posts, through: :kms
end

class Km < ActiveRecord::Base
  attr_accessible :count, :book_id, :post_id, :user_id
  belongs_to :book
  belongs_to :post
  belongs_to :user
end

这里的PostsController创建如下:

def create
  #this not working
  @post = current_user.build.kms(params[:post]) 
  @post.save
end

这是在视图表单上创建看起来像:

<%= form_for @post, :url => create_post_subdomain_path(@post), :method => :post  do |f| %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :year, "Year" %><br />
    <%= f.text_field :year %>
  </div>
  <div class="field">
    <%= f.label :author, "Author" %><br />
    <%= f.text_field :author %>
  </div>
  <div class="field">
    <%= f.label "Book" %><br />
    <%= hidden_field_tag "post[book_ids][]", nil %>
     <% Book.all.each_with_index do |book, index| %>
         <%= label_tag dom_id(book) do %>
          <%= check_box_tag "post[book_ids][]", book.id, @post.book_ids.include?(book.id), id: dom_id(book) %> <%= book.nama %>
          <% end %>&nbsp;
         <%= '<br/><br/>'.html_safe if index == 3 %>
    <% end %>

  </div>
    <br/>
  <div class="actions">
    <%= f.submit ( "Save" ), :class => "btn btn-inverse btn-medium" %>&nbsp;&nbsp; or &nbsp;&nbsp;<%= link_to 'Cancel', list_book_subdomain_path, :class => "btn btn-medium btn-primary" %>
  </div>
<% end %>

我在current_user.id创建km.user_id时尝试生成userpost

但是我收到了错误

Can't mass-assign protected attributes: name, year, author, book_ids

有什么建议吗?

提前谢谢

3 个答案:

答案 0 :(得分:1)

问题可能在于PostsController的创建操作。请尝试以下方法:

def create
  @post = Post.create(params[:post])
  @km = current_user.kms.build(:post_id => @post.id)
  @km.save
end

你仍然需要在Km上设置book_id,但我不清楚这是如何工作的,因为你的帖子说它可以has_many :books, :through => :kms,但是Km belongs_to :book。这没有意义。您对帖子上的book_ids做了什么?

答案 1 :(得分:0)

也许你想要

@post = Post.create(params[:post])
current_user.kms.create(post_id: @post.id) 

代替?

答案 2 :(得分:0)

<强>解决

我找到了一个解决方案,这样的解决方案: @post创建并更新user_id km所有列@post_id => @post.id,因此输出将是这样的

@post = Post.create(params[:post])
Km.where(:post_id => @post.id).update_all(:user_id => current_user.id)

<强>输出

#<Km id: 1, post_id: 1, user_id: 1>,
#<Km id: 2, post_id: 1, user_id: 1>,
#<Km id: 3, post_id: 1, user_id: 1>,