Rails中的未知属性 - 创建新对象

时间:2015-10-19 12:27:33

标签: ruby-on-rails database object attributes save

我正在研究我的Rails API。目前我想通过来自我的设备的后请求来保存对象。请求工作正常,但将数据保存到数据库时出现问题。 Rails说:

ActiveRecord::UnknownAttributeError (unknown attribute 'idea_id' for IdeaTag.):
  app/controllers/data_controller.rb:42:in `update_ideas'

所以,我知道这意味着它无法在“IdeaTag”中找到属性“idea_id”。

继承我的data_controller.rb

class DataController < ApplicationController
  skip_before_filter :verify_authenticity_token

  def token
    name=params[:owner]
    password=params[:password]

    @owner = Owner.authenticate_by_name(name, password)
    if @owner
      if @owner.user_uuid.blank?
        @user = User.new
        @user.token = SecureRandom.uuid
        @user.name = @owner.name
        @user.owner_uuid = @owner.uuid
        @user.created_at = Time.now
        @user.updated_at = Time.now
        @user.isLoggedIn = false
        @user.save!
      end

      @user = User.find_by_uuid(@owner.user_uuid)
      if @user.token.blank?
        token = SecureRandom.uuid
        @user.token = token
        @user.save
      end
    else
     render nothing: true, status: :unauthorized
    end
  end

  def update_ideas
    uuid = params[:uuid]
    text = params[:text]
    title = params[:title]
    owner_uuid = params[:owner_uuid]
    tag_id_1 = params[:tag_id_1]
    tag_id_2 = params[:tag_id_2]
    tag_id_3 = params[:tag_id_3]
    tag_id_4 = params[:tag_id_4]
    updated_at = params[:timeStamp]
    @idea = Idea.new(:uuid => uuid, :text => text, :title => title, :owner_uuid => owner_uuid, :tag_ids => [tag_id_1, tag_id_2, tag_id_3, tag_id_4], :updated_at => updated_at)
    @idea.save!
    render json: {:status => 200}
  end

  def getjson
    token = params[:token]
    @user = User.find_by_token(token)
    @users = User.all

    @owner = Owner.find_by_user_uuid(@user.uuid)
    @Owners = Owner.all

    ownerUuid = @owner.uuid

    @tags=Tag.all
    @ideas=Idea.where(:owner_uuid => ownerUuid)
    @votes=Vote.all
    @votings=Voting.all
  end

  # def token_auth
  #   token = params[:token]
  #   @owner = Owner.find_by_token(token)
  #   if @owner
  #     update_ideas
  #   end
  # end

end

错误发生在方法“update_ideas”以下行

  

@idea = Idea.new(:uuid =&gt; uuid,:text =&gt; te ...

理念模型:

class Idea < ActiveRecord::Base
  self.primary_key = :uuid
  has_many :idea_tags
  has_many :tags, through: :idea_tags
  belongs_to :voting
  has_many :votes
  belongs_to :owner
end

创意迁移文件

class CreateIdeas < ActiveRecord::Migration
  def change
    create_table :ideas, :id => false, :primary_key => :uuid do |i|
      i.string :uuid
      i.string :title
      i.string :text
      i.string :owner_uuid
      i.string :voting_uuid
      i.datetime :created_at
      i.datetime :updated_at
    end
  end
end

如何保存这样的对象?

1 个答案:

答案 0 :(得分:1)

由于您使用uuid作为创意的主键,我猜你在IdeaTag中有idea_uuid字段?如果是,则需要将foreign_key: 'idea_uuid添加到has_many :idea_tags,否则默认情况下假设foreign_key为idea_id。您可能还必须将其添加到belongs_to方法中。

has_many :idea_tags, foreign_key: 'idea_uuid'

http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html