简体   繁体   English

在Rails中创建多个一对多关系

[英]Creating multiple one to many relationships in rails

This question almost answers it, but I still think it's overkill. 这个问题几乎可以回答这个问题,但我仍然认为这太过分了。 Trouble with Rails has_many relationships Rails has_many关系麻烦

I really just want a to do assignments like this assign 我真的只想要a这样的作业

@user.browsing_location = location1
@user.home_location = location2

I've done a lot of googling around and all the information is contradictory, or explains setting up many to many relationships, and explains methods using an intermediary table. 我已经做了大量的谷歌搜索,所有的信息都是矛盾的,或者解释了建立多对多关系的过程,并解释了使用中间表的方法。 But really all the database should need is for the user table to have two differently names id fields for the locations. 但是实际上,数据库所需要做的只是让用户表在位置上具有两个不同的名称id字段。 Will something like the following work? 可以进行以下工作吗?

User Class 用户类别

  class User < ActiveRecord::Base
  #locations created by this user
  has_many :locations, :foreign_key => :creator_id

  #locations for browsing and visiting
  belongs_to :browsing_location, :source => :location
  belongs_to :home_location, :source => :location

end

Location Class 位置类别

class Location < ActiveRecord::Base
  #Users who are just browsing this location now
  has_many :browsing_users, :foreign_key => :browsing_location_id, :source => :users
  #Users who are living here now
  has_many :home_users, :foreign_key => :home_location_id, :source => :users

  #User who created this location
  has_one :user
end

Quite a lot of my models will need relationships like this so I would like to avoid having to create extra tables for this. 我的很多模型都需要这样的关系,因此我希望避免为此创建额外的表。

It appears that you're attempting to have two tables that inherit the location class, browsing_location and home_location and two tables that inherit the user class, browsing_user and home_user. 您似乎正在尝试拥有两个继承位置类的表,rowing_location和home_location,以及两个继承用户类的表,rowning_user和home_user。 For Rails 3: 对于Rails 3:

You've got the general idea, but it appears that you have mixed things up a bit. 您已经有了大致的想法,但似乎您已经把事情弄混了。 :source is used for many to many relationships to determine which association to use. :source用于多对多关系,以确定要使用哪个关联。 What you appear to need instead is :class_name 您似乎需要的是:class_name

I would need to see your table definitions for users and locations to make sure you're using :foreign_key attribute correctly. 我需要查看用户和位置的表定义,以确保正确使用:foreign_key属性。

user.rb

class User < ActiveRecord::Base
  # locations created by this user
  has_many :locations, :foreign_key => :creator_id

  # I'm assuming the locations belong to the user, as you're end goal was stated as  
  # the ability to call set user.browsing_location and user.home_location 
  # that would mean that browsing_location and home_location would need to belong to
  # the User class, not the other way around.  
  has_many :browsing_locations, :class_name => :location
  has_many :home_locations, :class_name => :location

end

class Location < ActiveRecord::Base

  # User who created the location
  belongs_to :user

  # Users who are just browsing this location now
  has_many :browsing_users, :class_name => :users
  # Users who are living here now
  has_many :home_users, :class_name => :users

end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM