Search code examples
ruby-on-railsruby-on-rails-3.2model-associations

Association has_many Rails Models


I have three models, user, post and book, associated through a fourth, kms.

  • Post model has many kms and books with through
  • Book model has many kms and posts with through
  • km belongs to book and post

I have tried this tutorial for the above case and successful.

Now, I want to create an association stating that a Post.

  • user has_many kms
  • Km belongs to user

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:

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

I try generate current_user.id to km.user_id when user create a post.

But I got the error

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

Any suggestions?

Thank you in advance


Solution

  • 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 = 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>,