Rails .save() 不存储 Id

发布于 2024-10-21 07:43:00 字数 2123 浏览 2 评论 0原文

我正在 Rails 中编写 RESTful API,当我创建 Label 对象并使用 .save() 方法时,标签会正确保存到数据库,但调用 @label.id< /code> 返回 null。

puts 语句验证除 id 字段之外的所有内容是否正确。我还验证了数据库具有正确的 ID。

另请注意:这是嵌套资源 URL 的内部:POST /members/:member_id/labels/

以前有人经历过吗?

def create
  if params[:member_id] and params[:title]

    # Periods in a username will have been translated to underscores
    member_id = params[:member_id].gsub('_', '.')
    title = params[:title]

    # @member = Member.find(member_id) # No longer in use
    @label = Label.new(:username => member_id, :title => title)

    if @label.save

      puts " *** label: #{@label.inspect}"
      @label.reload

      respond_to do |format|
        format.json { render :json => @label, :status => :created and return }
      end
    else
      respond_with :status => 406, :errors => @label.errors and return
    end

  end

  respond_with :status => 406, :errors => [ :message => "member_id and title parameters must be provided" ] and return
end

不幸的是,我无法按照要求提供迁移文件,因为我正在连接到最初通过 PHP 查看的现有 PostgreSQL 数据库。不过,下面是 create table 语句以及 select * 的外观。

DROP TABLE student_labels;
CREATE TABLE student_labels (
  id serial not null,
  username text not null,
  title text not null,
  created_at timestamp not null default now(),
  updated_at timestamp not null default now(),
  deleted_at timestamp
);
development=> select * from student_labels;
 id | username |    title     |         created_at         |         updated_at         | deleted_at
----+----------+--------------+----------------------------+----------------------------+------------
  7 | F0003    | Boom         | 2011-03-10 05:49:06.01771  | 2011-03-10 05:49:06.01771  |
  8 | F0003    | Boom         | 2011-03-10 05:49:28.659    | 2011-03-10 05:49:28.659    |
  9 | F0003    | Boom         | 2011-03-10 05:52:03.343815 | 2011-03-10 05:52:03.343815 |
 10 | F0003    | Boom         | 2011-03-10 05:53:07.803489 | 2011-03-10 05:53:07.803489 |

I'm writing a RESTful API in Rails and when I create a Label object and use the .save() method, the label is properly saved to the database but calling @label.id returns null.

The puts statement verifies that everything is correct except the id field. I have also verified that the database has the proper ids.

Also of note: this is inside of a nested resource URL: POST /members/:member_id/labels/.

Has anyone experienced this before?

def create
  if params[:member_id] and params[:title]

    # Periods in a username will have been translated to underscores
    member_id = params[:member_id].gsub('_', '.')
    title = params[:title]

    # @member = Member.find(member_id) # No longer in use
    @label = Label.new(:username => member_id, :title => title)

    if @label.save

      puts " *** label: #{@label.inspect}"
      @label.reload

      respond_to do |format|
        format.json { render :json => @label, :status => :created and return }
      end
    else
      respond_with :status => 406, :errors => @label.errors and return
    end

  end

  respond_with :status => 406, :errors => [ :message => "member_id and title parameters must be provided" ] and return
end

Unfortunately, I cannot provide a migration file as asked since I am connecting to an existing PostgreSQL database originally viewed through PHP. However, below is the create table statement along with what a select * looks like.

DROP TABLE student_labels;
CREATE TABLE student_labels (
  id serial not null,
  username text not null,
  title text not null,
  created_at timestamp not null default now(),
  updated_at timestamp not null default now(),
  deleted_at timestamp
);
development=> select * from student_labels;
 id | username |    title     |         created_at         |         updated_at         | deleted_at
----+----------+--------------+----------------------------+----------------------------+------------
  7 | F0003    | Boom         | 2011-03-10 05:49:06.01771  | 2011-03-10 05:49:06.01771  |
  8 | F0003    | Boom         | 2011-03-10 05:49:28.659    | 2011-03-10 05:49:28.659    |
  9 | F0003    | Boom         | 2011-03-10 05:52:03.343815 | 2011-03-10 05:52:03.343815 |
 10 | F0003    | Boom         | 2011-03-10 05:53:07.803489 | 2011-03-10 05:53:07.803489 |

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-10-28 07:43:00

问题是您的 id 列不是主键,因此数据库在请求最后插入行的 id 时不知道要提供什么内容。

The problem is that your id column isn't a primary key so the database doesn't know what to give rails when it asks for the id of the last inserted row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文