小编san*_*ank的帖子

如何使用依赖::在rails中销毁?

我有2个型号,如下所述.

class EmpGroup < ActiveRecord::Base
  belongs_to :user
  has_many :emp_group_members, dependent: :destroy
end
Run Code Online (Sandbox Code Playgroud)

class EmpGroupMember < ActiveRecord::Base
  belongs_to :emp_group
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

现在问题是每当我试图销毁一个组然后我收到如下错误.

PG::ForeignKeyViolation: ERROR:  update or delete on table "emp_groups" violates foreign key constraint "fk_rails_bd68440021" on table "emp_group_members"
DETAIL:  Key (id)=(1) is still referenced from table "emp_group_members".
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

ruby ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.2 ruby-on-rails-4

28
推荐指数
3
解决办法
3万
查看次数

如何在rails中指定数组中的限制和偏移量?

我有3个表PostText,PostImagePostVideo.现在我将来自上述三个表的数据组合成一个名为的单个数组userposts.

现在userposts我只想访问以offset 15开头的10条记录.

我怎样才能做到这一点?

我试过了userposts.first(10).它给了我前十条记录,但我想要从offset-15开始的10条记录.

提前致谢.

ruby arrays ruby-on-rails limit offset

10
推荐指数
2
解决办法
8605
查看次数

rails对于字符串是否与`parameterize`相反?

我用的parameterize方法.我想取消参数化.有没有办法做相反的事情parameterize

ruby ruby-on-rails

8
推荐指数
3
解决办法
4606
查看次数

如何为#<Book :: ActiveRecord_Relation:0x007fb709a6a8c0>解决未定义的方法`to_key'?

我遇到了未定义方法`to_key'的问题

这是我的books_controller.rb

 class BooksController  < ApplicationController
   def index
    @books = Book.where(user_id: current_user.id)
    end
 end
Run Code Online (Sandbox Code Playgroud)

和我的索引页面如下.

index.html.erb

<div>
  <%= form_for @books do |f| %>
...
...
    <% end %>
</div>
Run Code Online (Sandbox Code Playgroud)

现在,当我要访问索引页面时,我得到了如下错误.

undefined method `to_key' for #<Book::ActiveRecord_Relation:0x007fb709a6a8c0>
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-4

5
推荐指数
2
解决办法
2万
查看次数

如何在rails查询中传递数组作为参数?

我试图执行下面的查询,但出现错误。

Event.where("id IN ? and event_start_date > ?",[1, 2],Time.now)
Run Code Online (Sandbox Code Playgroud)

我收到错误如下:

SELECT "events".* FROM "events" WHERE (id IN 1,2 and event_start_date > '2015-03-11 04:40:43.819487')
PG::SyntaxError: ERROR:  syntax error at or near "1"
LINE 1: SELECT "events".* FROM "events" WHERE (id IN 1,2 and event_s...
Run Code Online (Sandbox Code Playgroud)

参数值不被视为数组。

ruby ruby-on-rails

0
推荐指数
1
解决办法
1284
查看次数