Ruby on rails 如何restfully设置具有多个变形的CRUD参数
使用has u许多uumorphs ActiveRecord插件处理双边多态关系的简单示例。我有两个班,“猫”和“狗”,它们可以彼此“交朋友”,所以总共有三个班:“猫”、“狗”和“交朋友”。猫可以和狗交朋友(是的,的确如此!),当然也可以和其他猫交朋友。狗也是如此。以下是我的模型:Ruby on rails 如何restfully设置具有多个变形的CRUD参数,ruby-on-rails,activerecord,plugins,polymorphism,Ruby On Rails,Activerecord,Plugins,Polymorphism,使用has u许多uumorphs ActiveRecord插件处理双边多态关系的简单示例。我有两个班,“猫”和“狗”,它们可以彼此“交朋友”,所以总共有三个班:“猫”、“狗”和“交朋友”。猫可以和狗交朋友(是的,的确如此!),当然也可以和其他猫交朋友。狗也是如此。以下是我的模型: class Cat < ActiveRecord::Base validates_presence_of :name end class Dog < ActiveRecord::Base v
class Cat < ActiveRecord::Base
validates_presence_of :name
end
class Dog < ActiveRecord::Base
validates_presence_of :name
end
class Friendship < ActiveRecord::Base
belongs_to :left, :polymorphic => true
belongs_to :right, :polymorphic => true
acts_as_double_polymorphic_join(
:lefts => [:cats, :dogs],
:rights => [:dogs, :cats]
)
end
或
这是我的schema.rb:
ActiveRecord::Schema.define(:version => 20090613051350) do
create_table "cats", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "dogs", :force => true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "friendships", :force => true do |t|
t.integer "left_id"
t.string "left_type"
t.integer "right_id"
t.string "right_type"
t.datetime "created_at"
t.datetime "updated_at"
end
end
我正试图创建一个新的“友谊”,例如,通过一个从URL获取left_uu;type和left_uuid,并从POST参数获取right_uuid和right_uid的路由,向/cats/3/关系发布帖子。路由将正确触发friendships控制器的#create方法,结果参数如下所示:
Parameters: {
"authenticity_token"=>"gG1eh2dXTuRPfPJ5eNapeDqJu7UJ5mFC/M5gJK23MB4=",
"left_type"=>"cats",
"right_type"=>"dogs",
"left_id"=>"3",
"right_id"=>"1"
}
出于这个问题范围之外的原因,我想在“左派”和“右派”之间进行区分,这样我就可以跟踪友谊的发帖人(因此“左派”和“右派”),但也许我的模型不是实现这一点的正确方法?我是否误解了has u许多uumorphs插件的用途?以下是#从友谊创建操作"控制器:
def create
@friendship= Friendship.new(
:left_type => params[:left_type],
:left_id => params[:left_id],
:right_type => params[:right_type],
:right_id => params[:right_id]
)
respond_to do |format|
if @friendship.save
format.xml {
render :xml => @friendship,
:status => :created,
:location => @friendship
}
else
format.xml {
render :xml => @friendship.errors,
:status => :unprocessable_entity
}
end
end
end
最后是my routes.rb:
map.resources :friendships
map.resources :cats do |cat|
cat.resources :friendships, :path_prefix => "/:left_type/:left_id"
end
map.resources :dogs do |dog|
dog.resources :friendships, :path_prefix => "/:left_type/:left_id"
end
正如我所说,我自己花了很长时间试图弄清楚这一点,但现有的文档要么是过时的、过于笼统的,要么是过于先进的——或者在某些情况下,这三者同时存在。我可以告诉你,要开始做RoR这件事并不容易!我知道RoR熟练有很多真正的好处,所以我会坚持下去,但有些人太差劲了,我开始秃顶了。我知道有很多经验丰富的Ruby/Rails开发人员,我希望有人能站出来,为我们这些凡人解释这件事
提前感谢,
JS我可能会选择猫和狗的模式,然后使用has_和_-allown_-to_-many关联
class Cat < ActiveRecord::Base
has_and_belongs_to_many :dogs
end
class Dog < ActiveRecord::Base
has_and_belongs_to_many :cats
end
你可以打电话给cat.dogs和dog.cats来寻找任何“友谊”。多态关系应该可以满足你的大部分需要,尽管如果你的猫和狗是猪的朋友,你想知道是谁发起了友谊,我以前的解决方案太简单了
class Animal < ActiveRecord::Base
belongs_to :creatures, :polymorphic => true
named_scope :cats, :conditions => {:creature_type => 'Cat'}
named_scope :dogs, :conditions => {:creature_type => 'Dog'}
named_scope :pigs, :conditions => {:creature_type => 'Pig'}
end
class Cat < ActiveRecord::Base
has_many :friends, :as => :creatures
end
class Dog < ActiveRecord::Base
has_many :friends, :as => :creatures
end
class Pig < ActiveRecord::Base
has_many :friends, :as => :creatures
end
我不确定这是否是您想要的,但我认为这是一种更简单的方法,简单总是更好。谢谢James,这听起来是一个更简单的解决方案!但是当我有两个以上的基类时,它会不会开始变得有点笨重?我要在另外两个班级加上第三个班级,比如说猪?而且,这个模型不允许我看到哪一方是关系的始作俑者(我上面例子中的“左派”),或者我误解了什么?
class Cat < ActiveRecord::Base
has_and_belongs_to_many :dogs
end
class Dog < ActiveRecord::Base
has_and_belongs_to_many :cats
end
create_table "cats_dogs", :id => false, :force => true do |t|
t.integer "cat_id"
t.integer "dog_id"
t.datetime "created_at"
t.datetime "updated_at"
end
class Animal < ActiveRecord::Base
belongs_to :creatures, :polymorphic => true
named_scope :cats, :conditions => {:creature_type => 'Cat'}
named_scope :dogs, :conditions => {:creature_type => 'Dog'}
named_scope :pigs, :conditions => {:creature_type => 'Pig'}
end
class Cat < ActiveRecord::Base
has_many :friends, :as => :creatures
end
class Dog < ActiveRecord::Base
has_many :friends, :as => :creatures
end
class Pig < ActiveRecord::Base
has_many :friends, :as => :creatures
end
class Cat < ActiveRecord::Base
has_many :friends, :as => :creatures
has_many :dogs, :through => :animal
has_many :pigs, :through => :animal
end