kilik52 2008-12-19 23:24
浏览 271
已采纳

rails中怎么删除一个没有primary_key的记录?

比如一个model是join table,并且有些自己的属性。
[code="rails"]
class CreateFriendsEducations < ActiveRecord::Migration
def self.up
create_table :friends_educations, :id => false do |t|
t.integer :friend_id, :null => false
t.integer :school_id, :null => false
t.string :major_in
t.string :minor_in

t.timestamps
end
end

def self.down
drop_table :friends_educations
end
end
[/code]

要删除该table中某一项怎么删呢。。
我用
[code="rails"]FriendsEducation.destroy_all("some_conditions")[/code]
[code="rails"]
@edu = FriendsEducation.find("some_conditions")
@edu.destroy
[/code]

数据库都没反应。。。log显示
[4;35;1mFriendsEducation Destroy (0.0ms)[0m [0mDELETE FROM "friends_educations" WHERE "id" = NULL [0m

  • 写回答

2条回答 默认 最新

  • 冠仔 2008-12-20 16:20
    关注

    补充下:destroy是根据id来删除的

    在friend.rb里面设置
    has_many :friends_educations,:dependent => :destroy

    a=friend.first
    a.destroy会连带把
    :friends_educations里面对应数据删除
    你也可以
    a.friends_educations.find(id).destroy删除单独某项

    别在和我犯同样的错误了。。用上ID吧

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要