如何从一个表到另一个表正确地进行活动记录查询?

时间:2014-03-25 06:39:31

标签: ruby-on-rails ruby activerecord associations

整个下午/晚上都被困在这。我将继续努力,真的很感激帮助。

我有两张不同的牌桌。帖子(状态和照片)和地点(地址)。我想将这些(状态,照片,地址)合并为一个表单和一个节目。

所以,我认为我在制作Active Record Query Interface时遇到了麻烦。但是,我可能搞砸了关联表......

Post.rb模型

class Post < ActiveRecord::Base

  belongs_to :place
  belongs_to :user
  has_many :comments
  has_many :commenters, through: :comments, source: :user
end

Place.rb模型

class Place < ActiveRecord::Base

  has_many :posts

end

帖子_form.html.erb

<%= form_for(@post) do |f| %>
  <% if @post.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>

      <ul>
      <% @post.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :status %><br>
    <%= f.text_field :status %>
  </div>
  <div class="field">
    <%= f.label :upload %><br>
    <%= f.text_field :upload %>
  </div>
    <%= f.label :place %><br>
    <%= f.text_field :place %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

帖子show.html.erb

<h1><%= @post.status %></h1>
<p><%= link_to @post.upload, @post.upload %></p>
<p><%= @post.place %></p>

<%= form_for [@post, @comment] do |f| %>
  <p>
    <%= f.text_area :response, placeholder: "Add a comment...", :rows => 8, :cols => 40 %>
  </p>
  <%= f.submit %>
<% end %>

<ul class="comments">
  <% @post.comments.each do |comment| %>
    <li><%= "#{comment.user.try(:email)} posted: #{comment.response}" %></li>
  <% end %>
</ul>

<p><%= link_to "Back to home page", root_path %></p>

posts_controller.rb

class PostsController < ApplicationController
  before_action :set_post, only: [:show, :edit, :update, :destroy]

  # GET /posts
  # GET /posts.json

  def index
    @posts = Post.all
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find params[:id]
    @comment = Comment.new(:post => @post)
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
  end


    def create
    safe_post = params.require(:post).permit(:status, :upload)
    @post = current_user.posts.new safe_post
    @post.place = Place.from_params params[:place]
    @post.save
    redirect_to @post
  end


  # PATCH/PUT /posts/1
  # PATCH/PUT /posts/1.json
  def update
    respond_to do |format|
      if @post.update(post_params)
        format.html { redirect_to @post, notice: 'Post was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @post.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /posts/1
  # DELETE /posts/1.json
  def destroy
    @post.destroy
    respond_to do |format|
      format.html { redirect_to posts_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_post
      @post = Post.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def post_params
      params.require(:post).permit(:status, :upload)
    end
end

places_controller.rb

class PlacesController < ApplicationController
  before_action :set_place, only: [:show, :edit, :update, :destroy]

  # GET /places
  # GET /places.json
  def index
    @places = Place.all
  end

  # GET /places/1
  # GET /places/1.json
  def show

    @place = Place.find params[:id]

  end

  # GET /places/new
  def new
    @place = Place.new
  end

  # GET /places/1/edit
  def edit
  end

  # POST /places
  # POST /places.json
  def create
    @place = Place.new(place_params)

    respond_to do |format|
      if @place.save
        format.html { redirect_to @place, notice: 'Place was successfully created.' }
        format.json { render action: 'show', status: :created, location: @place }
      else
        format.html { render action: 'new' }
        format.json { render json: @place.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /places/1
  # PATCH/PUT /places/1.json
  def update
    respond_to do |format|
      if @place.update(place_params)
        format.html { redirect_to @place, notice: 'Place was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @place.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /places/1
  # DELETE /places/1.json
  def destroy
    @place.destroy
    respond_to do |format|
      format.html { redirect_to places_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_place
      @place = Place.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def place_params
      params.require(:place).permit(:address, :latitude, :longitude)
    end
end

schema.rb

ActiveRecord::Schema.define(version: 20140324213459) do

  create_table "comments", force: true do |t|
    t.integer  "user_id"
    t.integer  "post_id"
    t.string   "response"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "comments", ["post_id"], name: "index_comments_on_post_id"
  add_index "comments", ["user_id"], name: "index_comments_on_user_id"

  create_table "places", force: true do |t|
    t.string   "address"
    t.float    "latitude"
    t.float    "longitude"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "posts", force: true do |t|
    t.string   "status"
    t.string   "upload"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_id"
    t.integer  "place_id"
  end

  add_index "posts", ["place_id"], name: "index_posts_on_place_id"
  add_index "posts", ["user_id"], name: "index_posts_on_user_id"

  create_table "users", force: true do |t|
    t.string   "name"
    t.string   "photo"
    t.string   "bio"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

的routes.rb

DondeApp::Application.routes.draw do


  resources :posts do
    resources :comments, only: :create
    resources :places
  end

  devise_for :users
   root 'posts#index'

  end

2 个答案:

答案 0 :(得分:0)

您需要嵌套的模型表单,请查看此http://railscasts.com/episodes/196-nested-model-form-part-1

答案 1 :(得分:0)

您似乎尝试访问@post.place _form.html.erb中可能通过AREL魔法实际工作的text_field,但您可能会得到一些奇怪的结果,将其放入jQuery 。通常,您可以使用选择框从现有位置中进行选择,或者添加show控件以找到正确的参考。

因此,Post操作中的查询实际上可以生成合适的create对象,因为它已在Place.from_params操作中正确设置。你确实在那里使用params[:place]创建了一个代码,使用似乎不允许的神秘Post(对于text_field对象)。由于您的表单只包含place来引用Post,我怀疑这是否有用。

查看rails控制台中的Place个对象,然后查看引用有效的place对象。如果他们这样做,您必须在视图中反映这一点,访问@post对象的text_field属性将返回一个整体对象,该对象无法“知道”如何在includes(:place)中展示自己更不用说从一个人那里创造自己了。

猜测这是你遇到的主要问题,但很难说你是不是告诉我们你的期望和你得到的东西。

鉴于此,您可以优化您的AREL查询以使用n+1以避免index查询问题,但这只适用于您的{{1行动。否则我认为 AREL主要涵盖你。