【问题标题】:Association has_many Rails Models关联 has_many Rails 模型
【发布时间】:2013-04-11 11:38:25
【问题描述】:

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

  • 后模型有许多公里和书籍通过
  • 图书模型有许多公里和帖子通过
  • km 属于书帖

上述案例我试过this tutorial,成功了。

现在,我想创建一个关联来声明一个帖子。

  • 用户有_many kms
  • 公里属于用户

协会:

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 %>

user 创建post 时,我尝试生成current_user.idkm.user_id

但我得到了错误

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

有什么建议吗?

提前谢谢你

【问题讨论】:

  • 你的问题到底是什么?
  • 您的意图目前还不清楚。你想做什么?请重写你想要发生的事情。其余的都是有道理的。
  • user 创建post 时,我尝试生成current_userkm,但不工作,我收到错误Can't mass-assign protected attributes: name, year, author, book_ids

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


【解决方案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 =&gt; :kms,但 Km belongs_to :book。那没有意义。你在用帖子上的book_ids 做什么?

【讨论】:

  • 书籍和帖子没有问题,我只想在user创建post时生成current_user.idkm.user_id
  • 以上将做到这一点。 current_user.kms.build() 将使用 user_id == current_user.id 创建一个新的 km。我只是忘了保存@km。
  • 我已经尝试过你的解决方案,但是 user_id 没有填写,而是创建了一个新行,看截图this
  • 您的 current_user.id 如何评估?当然,您也可以简单地做Km.create(post_id: @post.id, user_id: current_user.id),但我看不出它不应该起作用的原因。如果 current_user 是具有 ID 的现有对象,current_user.kms.build 应创建一个空 Km,并将 user_id 设置为 current_user.id
【解决方案2】:

也许你想要

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

改为?

【讨论】:

  • 谢谢,但不是这样,当user 创建post 时,我想生成current_user.idkm.user_id
【解决方案3】:

已解决

我找到了一个解决方案,一个像这样的解决方案: @postkm 上创建和更新所有列user_id @post_id =&gt; @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>,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多