[英]Association has_many Rails Models
I have three models, user, post and book, associated through a fourth, kms. 我有四个模型,分别是用户,帖子和书籍,分别通过公里数来关联。
I have tried this tutorial for the above case and successful. 对于上述情况,我已经尝试过本教程并成功。
Now, I want to create an association stating that a Post. 现在,我想创建一个说明该帖子的关联。
Associations: 协会:
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
Here's create on PostsController looks like: 这是在PostsController上创建的:
def create
#this not working
@post = current_user.build.kms(params[:post])
@post.save
end
And here's on view form to create looks like: 这是在视图窗体上创建的样子:
<%= 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 %>
<%= '<br/><br/>'.html_safe if index == 3 %>
<% end %>
</div>
<br/>
<div class="actions">
<%= f.submit ( "Save" ), :class => "btn btn-inverse btn-medium" %> or <%= link_to 'Cancel', list_book_subdomain_path, :class => "btn btn-medium btn-primary" %>
</div>
<% end %>
I try generate current_user.id
to km.user_id
when user
create a post
. 当
user
创建post
时,我尝试将current_user.id
成为km.user_id
。
But I got the error 但是我得到了错误
Can't mass-assign protected attributes: name, year, author, book_ids
Any suggestions? 有什么建议么?
Thank you in advance 先感谢您
The problem is probably in the create action of your PostsController. 问题可能出在PostsController的create动作中。 Try the following:
请尝试以下操作:
def create
@post = Post.create(params[:post])
@km = current_user.kms.build(:post_id => @post.id)
@km.save
end
You will still have to set a book_id on the Km, but it's not really clear to me how that will work because your post says it can has_many :books, :through => :kms
, but a Km belongs_to :book
. 您仍然必须在Km上设置一个book_id,但是我对它的工作方式还不清楚,因为您的帖子说它可以具有
has_many :books, :through => :kms
,但是Km belongs_to :book
。 That doesn't make sense. 那没有道理。 What are you doing with the
book_ids
on the Post? 您在
book_ids
上的book_ids
正在做什么?
Maybe you want 也许你想要
@post = Post.create(params[:post])
current_user.kms.create(post_id: @post.id)
instead? 代替?
Solved 解决了
I found a solution, a solution like this : @post
create and update all column user_id
on km
where @post_id => @post.id
, so the output will be like this 我找到了一个解决方案,像这样的解决方案:
@post
创建并更新km
上的所有列user_id
,其中@post_id => @post.id
,因此输出将是这样的
@post = Post.create(params[:post])
Km.where(:post_id => @post.id).update_all(:user_id => current_user.id)
Output 产量
#<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>,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.