如何在两个导轨模型之间创建关联 [英] How to create an association between two rails models

查看:60
本文介绍了如何在两个导轨模型之间创建关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个新手问题,但我仍在学习如何在 rails 中创建两个模型之间的关联.我有一个用户模型和一个 journal_entry 模型.日记账分录属于用户,用户有_many 日记账分录.我创建了如下所示的迁移:

class AddJournalEntriesToUsers <ActiveRecord::迁移定义改变add_column :journal_entries, :user_id, :integer结尾结尾类 AddIndexToJournalEntries 

这是我的用户模型的样子:

class User 应该匹配确认", :if =>:密码validates_length_of :password, :minimum =>3, :message =>密码长度必须至少为 3 个字符", :if =>:密码validates_presence_of :password, :on =>:创造validates_presence_of :emailvalidates_uniqueness_of :email结尾

这是我的 journal_entry 模型的样子:

class JournalEntry 

但是当我在 /journal_entries/new 创建一个新的日志条目时,我只是一个验证错误,上面写着用户不能为空".因此,即使我已登录并且我的 db/schema.rb 中有一个 user_id 列,user_id 也不会添加到日志条目中:

create_table "journal_entries", :force =>真的做 |t|t.string "标题"t.text "post"t.datetime "created_at", :null =>错误的t.datetime "updated_at", :null =>错误的t.integer "user_id"结尾

此外,这是我在 journal_entries/new 上使用的用于创建日志条目的表单:

<%= form_for(@journal_entry) do |f|%><% 如果@journal_entry.errors.any?%><div id="error_explanation"><h2><%=pluralize(@journal_entry.errors.count, "error") %>禁止保存此日志条目:</h2><ul><% @journal_entry.errors.full_messages.each do |msg|%><li><%=msg%></li><%结束%>

<%结束%><div class="field"><%= f.label :title%><br/><%= f.text_field :title %>

<div class="field"><%= f.label :post %><br/><%= f.text_area :post %>

<div class="actions"><%= f.submit %>

<%结束%>

我在这里错过了什么?我需要在表单上添加 user_id 作为隐藏字段吗?

解决方案

我敢打赌,你忘记了

def 创建@journal_entry = @user.journal_entries.build(params[:journal_entry])# @journal_entry = current_user.journal_entries.build(params[:journal_entry])如果@journal_entry.save..

This is a newbie question, but I'm still learning how to create an association between two models in rails. I have a user model and a journal_entry model. The journal entries belong to the user and the user has_many journal entries. I've created migrations that look like this:

class AddJournalEntriesToUsers < ActiveRecord::Migration
  def change    
    add_column :journal_entries, :user_id, :integer
  end
end

class AddIndexToJournalEntries < ActiveRecord::Migration
  def change
    add_index :journal_entries, [:user_id, :created_at]
  end
end

Here's what my User model looks like:

class User < ActiveRecord::Base
  authenticates_with_sorcery!

  attr_accessible :email, :password, :password_confirmation

  has_many :journal_entries, dependent: :destroy

  validates_confirmation_of :password, :message => "should match confirmation", :if => :password
  validates_length_of :password, :minimum => 3, :message => "password must be at least 3 characters long", :if => :password
  validates_presence_of :password, :on => :create
  validates_presence_of :email
  validates_uniqueness_of :email

end

And here's what my journal_entry model looks like:

class JournalEntry < ActiveRecord::Base
  attr_accessible :post, :title, :user_id

  belongs_to :user

  validates :user_id, presence: true

  default_scope order: 'journal_entries.created_at DESC'
end

But when I go to create a new journal entry at /journal_entries/new I just a validation error that says "User can't be blank". So the user_id is not getting added to the journal entry even though I'm logged in and there is a user_id column in my db/schema.rb:

create_table "journal_entries", :force => true do |t|
    t.string   "title"
    t.text     "post"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
    t.integer  "user_id"
  end

Also, this is the form that I'm using on journal_entries/new to create the journal entry:

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

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

  <div class="field">
    <%= f.label :title %><br />
    <%= f.text_field :title %>
  </div>
  <div class="field">
    <%= f.label :post %><br />
    <%= f.text_area :post %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

What am I missing here? Do I need to add the user_id as a hidden field on the form?

解决方案

i bet, that u forget something like

def create
    @journal_entry = @user.journal_entries.build(params[:journal_entry])
    # @journal_entry = current_user.journal_entries.build(params[:journal_entry])
    if @journal_entry.save
    ..

这篇关于如何在两个导轨模型之间创建关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆