Ruby on rails 协会有许多Rails模型

Ruby on rails 协会有许多Rails模型,ruby-on-rails,ruby-on-rails-3.2,model-associations,Ruby On Rails,Ruby On Rails 3.2,Model Associations,我有三个模型,用户、帖子和书籍,通过第四个模型kms关联 Post model有许多KM和具有直通功能的书籍 图书模型有很多KM和文章,都是通过 知识管理属于图书和邮政 我已经尝试了上述案例并取得了成功 现在,我想创建一个关联,声明一篇文章 用户拥有多个KM Km属于用户 协会: class User < ActiveRecord::Base include Tenantable::Schema::Model has_many :kms, dependent: :destr

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

  • Post model有许多KM和具有直通功能的书籍
  • 图书模型有很多KM和文章,都是通过
  • 知识管理属于图书和邮政
我已经尝试了上述案例并取得了成功

现在,我想创建一个关联,声明一篇文章

  • 用户拥有多个KM
  • 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
下面是要创建的视图表单,如下所示:

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 %>
create_post_subdomain_path(@post),:method=>:post do | f |%>





“btn btn反向btn中端”%>或“btn btn中端btn主端”%>

user
创建
post
时,我尝试生成
current\u user.id
km.user\u id

但是我弄错了

无法批量分配受保护的属性:姓名、年份、作者、图书ID

有什么建议吗

提前谢谢你

也许你想要

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

也许你想要

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

相反?

问题可能出在PostsController的创建操作中。请尝试以下操作:

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

你仍然需要在Km上设置一个图书id,但我并不清楚它是如何工作的,因为你的帖子说它可以
有很多:图书,:通过=>:kms
,但是Km
属于:book
。那没有道理。您如何使用Post上的
图书ID
呢?

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

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

你仍然需要在Km上设置一个图书id,但我并不清楚它是如何工作的,因为你的帖子说它可以
有很多:图书,:通过=>:kms
,但是Km
属于:book
。那没有道理。你在用帖子上的
图书ID
做什么?

已解决

我找到了一个解决方案,像这样的解决方案:
@post
创建并更新
km
上的所有列
user\u id
,其中
@post\u 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>,
#,
#,
#,

已解决

我找到了一个解决方案,像这样的解决方案:
@post
创建并更新
km
上的所有列
user\u id
,其中
@post\u 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>,
#,
#,
#,

您的问题是什么?您的意图目前还不清楚。你想干什么?请重写你想发生的事情。其余的都有道理。我尝试生成
current_user
km
user
创建一个
post
时,我遇到了一个错误
无法批量分配受保护的属性:姓名、年份、作者、图书ID
你的问题到底是什么?你的意图目前还不清楚。你想干什么?请重写你想发生的事情。其余的都有道理。我尝试生成
current_user
km
user
创建一个
post
时,但没有工作,我得到了错误
无法批量分配受保护的属性:姓名、年份、作者、图书ID
谢谢,但不是那样,当
user
创建
post
时,我想生成
current\u user.id
km.user\u id
km.user\u id
创建
post
时,我想生成
current\u user.id
km.user\u id
创建
post
。书籍和帖子没有问题,我只想在
用户创建
post
时生成
current\u user.id
。上述操作即可完成。current_user.kms.build()将使用user_id==current_user.id创建一个新的km。我只是忘了保存@km。我尝试了你的解决方案,但用户id没有填写,而是创建了一个新行,看截图,你当前的用户id是如何评估的?当然,您也可以简单地执行
Km.create(post\u id:@post.id,user\u id:current\u user.id)
,但我看不出它不起作用的原因。如果当前用户是一个ID为的现有对象,
current\u user.kms.build
应该创建一个空Km,并将
user\u ID
设置为
current\u user.ID
。图书和帖子没有问题,我只想在
用户创建
post
时生成
current\u user.id
。上述操作即可完成。current_user.kms.build()将使用user_id==current_user.id创建一个新的km。我只是忘了保存@km。我尝试了你的解决方案,但用户id没有填写,而是创建了一个新行,看截图,你当前的用户id是如何评估的?当然,您也可以简单地执行
Km.create(post\u id:@post.id,user\u id:current\u user.id)
,但我看不出它不起作用的原因。如果当前用户是一个ID为的现有对象,
current\u user.kms.build
应创建一个
user\u ID
设置为
current\u user.ID
的空Km。