比如一个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
补充下:destroy是根据id来删除的
在friend.rb里面设置
has_many :friends_educations,:dependent => :destroy
a=friend.first
a.destroy会连带把
:friends_educations里面对应数据删除
你也可以
a.friends_educations.find(id).destroy删除单独某项
别在和我犯同样的错误了。。用上ID吧
经过我实例证明
如果不改rails内核的话是删除不了的
总结出来的结论是
不要把:id取消
那不符合ror的模式