代码之家  ›  专栏  ›  技术社区  ›  rails_id

关联有多个Rails模型(_M)

  •  1
  • rails_id  · 技术社区  · 13 年前

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

    • 后模型有许多公里和带直通的书籍
    • 书本模型有许多带有直通的公里和柱子
    • 公里属于书本和邮政

    我试过了 this tutorial 针对上述案例并取得成功。

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

    • 用户拥有任意公里数(_M)
    • 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 什么时候 user 创建 post .

    但我弄错了

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

    有什么建议吗?

    提前谢谢

    3 回复  |  直到 13 年前
        1
  •  1
  •   Jaap Haagmans    13 年前

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

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

    你仍然需要在公里上设置一个book_id,但我并不清楚这将如何工作,因为你的帖子说它可以 has_many :books, :through => :kms ,但一公里 belongs_to :book 。那没有道理。你在用这个做什么 book_ids 在邮报上?

        2
  •  0
  •   fotanus    13 年前

    也许你想要

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

    相反

        3
  •  0
  •   rails_id    13 年前

    解决了的

    我找到了一个解决方案,一个这样的解决方案: @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>,
    
    推荐文章