rails 4不要将数据发送到连接表

时间:2013-11-21 16:55:20

标签: ruby-on-rails ruby ruby-on-rails-4

我想要一个用户,可以为另一个用户撰写评论,所以我创建了2个表,评论:内容 迁移

class CreateReviews < ActiveRecord::Migration
  def change
    create_table :reviews do |t|
      t.text :content

      t.timestamps
    end
  end
end

users_reviews

class CreateUsersReviews < ActiveRecord::Migration
  def change
    create_table :users_reviews do |t|
      t.references :for_user, index: true
      t.references :by_user, index: true

      t.timestamps
    end
  end
end

我忘了将review_id创建到Users_review

class AddReviewIdToUsersReviews < ActiveRecord::Migration
  def change
    add_reference :users_reviews, :review, index: true
  end
end

然后我创建关联

class User < ActiveRecord::Base

  has_many :users_review
  has_many :reviews, :through => :users_review

class Review < ActiveRecord::Base
    has_many :users_reviews
    has_many :users, :through => :users_reviews
end

class UsersReview < ActiveRecord::Base
  belongs_to :user
  belongs_to :review
end

然后是控制器

class ReviewsController < ApplicationController

    def new
        @user = User.find(params[:user_id])
        @review = @user.reviews.new(params[:user_id])
    end

    def create
        @user = User.find(params[:user_id])
        @review = @user.reviews.build(review_params)
        @review.save
        redirect_to root_path
    end

    def show
    end

    def index
        @user = User.find(params[:for_user_id])
        @reviews = Review.all
    end

    private
    def review_params
        params.require(:review).permit(:for_user_id, :by_user_id, :content)
    end

end

和我的观点

<%= form_for([@user, @user.reviews.build]) do |f| %>
<%= f.text_area :content, placeholder: "Your review" %>
<%= f.submit "GO", class: "btn btn-large btn-primary" %>
<% end %>

rails将数据发送到Review表,但是不发送连接表,user_review是空的? 我做错了什么?

1 个答案:

答案 0 :(得分:1)

我给你这个解决方案,与你的略有不同......但这只是一个建议。

我更喜欢坚持使用连接表的字母顺序...然后使用has_many :through我明确地给出:user_id ...

我希望这会有所帮助......

class CreateStuff < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name

      t.timestamps
    end
    create_table :reviews do |t|
      t.belongs_to :user
      t.integer :for_user_id
      t.text :content

      t.timestamps
    end
    create_table :reviews_users do |t|
      t.belongs_to :user
      t.belongs_to :review

      t.timestamps
    end
  end
end

class User < ActiveRecord::Base
  has_many :reviews_users
  has_many :reviews, through: :reviews_users

  def received_reviews
    Review.where(for_user: self)
  end
end

class Review < ActiveRecord::Base
  belongs_to :user
  belongs_to :for_user, class_name: "User"
end

class ReviewsUser < ActiveRecord::Base
  belongs_to :user
  belongs_to :review
end

在控制台中:

User.new(name: 'bar').save
User.new(name: 'foo').save

bar = User.first
foo = User.last

foo.reviews.create user:foo, for_user:bar, content:'foo bar!'

foo.reviews
# => [#<Review id: 1, user_id: 2, for_user_id: 1, content: "foo bar!", created_at: "2013-11-21 19:45:56", updated_at: "2013-11-21 19:45:56">]>

Review.last
# => #<Review id: 1, user_id: 2, for_user_id: 1, content: "foo bar!", created_at: "2013-11-21 19:45:56", updated_at: "2013-11-21 19:45:56">

Review.last.user
# => #<User id: 2, name: "foo", created_at: "2013-11-21 19:38:33", updated_at: "2013-11-21 19:38:33">

Review.last.for_user
# => #<User id: 1, name: "bar", created_at: "2013-11-21 19:38:33", updated_at: "2013-11-21 19:38:33">

User.first.received_reviews
# => #<ActiveRecord::Relation [#<Review id: 2, user_id: 2, for_user_id: 1, content: "foo bar!", created_at: "2013-11-21 19:45:56", updated_at: "2013-11-21 19:45:56">]>