Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails协会-最佳实践的想法_Ruby On Rails_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails Rails协会-最佳实践的想法

Ruby on rails Rails协会-最佳实践的想法,ruby-on-rails,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby On Rails 3,Activerecord,关于Rails关联的问题。 考虑以下模型: 人 事件 图像 人物和事件可以有许多图像。 上传到活动的图像需要能够与多人关联 这意味着人和图像之间存在两种关系。 其中图像直接上传到个人身上。在一个事件中,一个人被贴上标签 根据事件中一个或多个图像中标记的事实,个人和事件之间是否也存在关系?在这方面,它是一种图像标记系统,根据人们标记的事件创建关联 想知道Rails中创建这种关联的最佳实践是什么?非常感谢您的任何帮助或建议 我不知道这是否是最好的方法,但我会这样做: class User <

关于Rails关联的问题。 考虑以下模型:

人 事件 图像 人物和事件可以有许多图像。 上传到活动的图像需要能够与多人关联

这意味着人和图像之间存在两种关系。 其中图像直接上传到个人身上。在一个事件中,一个人被贴上标签

根据事件中一个或多个图像中标记的事实,个人和事件之间是否也存在关系?在这方面,它是一种图像标记系统,根据人们标记的事件创建关联


想知道Rails中创建这种关联的最佳实践是什么?非常感谢您的任何帮助或建议

我不知道这是否是最好的方法,但我会这样做:

class User < ActiveRecord::Base
  has_many :images, :as => :owner
  has_many :tags
  has_many :tagged_images, :through => :tags, :source => :image

  has_many :tagged_events, :finder_sql => %q( SELECT `events`.* FROM `events`, `images` WHERE `events`.id = `images`.owner_id AND `images.owner_type ="Event" `
                                    WHERE (`images`.id IN (#{tagged_image_ids.join(',')}) ))


end

class Event < ActiveRecord::Base
  has_many :images, :as => :owner

  has_many :tagged_users, :finder_sql => %q( SELECT `users`.* FROM `users`, `images` WHERE `users`.id = `images`.owner_id AND `images.owner_type ="User" `
                                    WHERE (`images`.id IN (#{image_ids.join(',')}) ))


end

class Tag < ActiveRecord::Base
  belongs_to :user
  belongs_to :image  
end

class Image < ActiveRecord::Base
  belongs_to :owner, :polymorphic => true
  has_many :tags
  has_many :tagged_users, :through => :tags, :source => :user
end

注意:使用finder_sql并不是Rails中最好的,因为例如,它不允许您仅使用关系添加新的标记图像,但对于阅读来说效果很好。

我不能绝对确定最佳实践, 但据我所知,有机会以简单的方式解决您的案件:

我们有:

Event ... has_many :photos; Photo ... belongs_to :event
解决方案是创建一些中间ActiveRecord类

People ... has_many :photos, :through photo_mappings

PhotoMapping ... belongs_to :user; belongs_to :photo

有一个很好的AR关联最佳实践1

在rails中,定义一个包含额外字段的联接表是绝对可能的。因此,在本例中,我将定义下表:

class LinkedImage
  belongs_to :person
  belongs_to :image

  OWNER=1
  TAGGED=2

  validates :relation_type, :inclusion => {:in => [OWNER, TAGGED]}
end
这个表将图像链接到一个人,并且有一个额外的字段关系类型,您可能会想到一个更合适的名称,它现在可以有两个值:1为所有者,表示图像直接上载到该人,2为图像中的该人添加了标签。除了关系之外,也许你想存储一些额外的东西,比如图像中的位置,一条额外的注释,然后你可以很容易地在这里添加它

这个人看起来像:

class Person
  has_many :linked_images
  has_many :uploaded_images, :class_name => Image, 
                             :through => :linked_images, 
                             :conditions => "relation_type=#{LinkedImage::OWNER}"
  has_many :tagged_in_images, :class_name => Image,
                              :through => :linked_images,
                              :conditions => 'relation_type=#{LinkedImage::TAGGED}"
end 
图像的代码可能是类似的


我希望这能有所帮助。

但如果这样做的话,你就假设每张照片都上传到了活动中。如果我明白good@galaxy也在询问用户的照片。我们可能只能猜测,最后一个字还是在galaxy下面@这是正确的。照片上载到人物和事件,并在事件中标记。