Ruby on rails 保存用户的多个首选项

Ruby on rails 保存用户的多个首选项,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我需要能够保存用户的偏好,我想知道我是否在正确的轨道上 以下示例: 桌子 用户:你有很多偏好吗 偏好:属于用户,有很多煎饼,有很多冰淇淋 煎饼:属于偏好 冰淇淋:属于偏好 这些表格包含以下内容: 用户:姓名、电子邮件、电话 首选项:用户id、煎饼id、冰淇淋id 煎饼:名称(即whipStream) 冰淇淋:名称(即草莓) 现在,在用户创建其常规个人资料(姓名、电子邮件、性别、电话号码等)后 我想将他们重定向到一个页面,让他们填写他们的食物偏好 用户可以喜欢多种冰淇淋 我是否使用正确的逻

我需要能够保存用户的偏好,我想知道我是否在正确的轨道上

以下示例:

桌子

  • 用户:你有很多偏好吗
  • 偏好:属于用户,有很多煎饼,有很多冰淇淋
  • 煎饼:属于偏好
  • 冰淇淋:属于偏好
这些表格包含以下内容:

  • 用户:姓名、电子邮件、电话
  • 首选项:用户id、煎饼id、冰淇淋id
  • 煎饼:名称(即whipStream)
  • 冰淇淋:名称(即草莓)
现在,在用户创建其常规个人资料(姓名、电子邮件、性别、电话号码等)后 我想将他们重定向到一个页面,让他们填写他们的食物偏好

用户可以喜欢多种冰淇淋

我是否使用正确的逻辑让用户填写他们的偏好

有没有更好的方法可以实现保存用户偏好的目标

我现在这样做的结果应该是这样的>>

偏好

id,用户id,煎饼id,冰淇淋id

1,2[1,2],[3,5,7,2]

这有效吗


谢谢。

我可能会用一些更简单的东西——通过许多关联来模拟你的现实

class User < ActiveRecord::Base
  has_many :preferedicecreams
  has_many :icecreams, through: :preferedicecreams
end

class PreferedIcecream < ActiveRecord::Base
  belongs_to :user
  belongs_to :icecream
end

class Icecream < ActiveRecord::Base
  has_many :preferedicereams
  has_many :users, through: :preferedicereams
end 
class用户

煎饼也是一样。

我可能会用一些更简单的东西——有很多关联来模拟你的现实

class User < ActiveRecord::Base
  has_many :preferedicecreams
  has_many :icecreams, through: :preferedicecreams
end

class PreferedIcecream < ActiveRecord::Base
  belongs_to :user
  belongs_to :icecream
end

class Icecream < ActiveRecord::Base
  has_many :preferedicereams
  has_many :users, through: :preferedicereams
end 
class用户

煎饼也是一样。

首先,我想我们可以修改模式如下:

  • 用户
    有许多
    偏好
  • Preference
    通过
    PreferencePancake
    有许多
    Pancake
    ,因此
    PreferencePancake
    Preference
    Pancake
    的联接表
  • Preference
    通过
    PreferenceIcecream
    有许多
    Icecream
    ,因此
    PreferenceIcecream
    Preference
    Icecream
    的联接表
我为什么建议这样做

  • 一个
    煎饼
    冰淇淋
    是存在的,而不关心
    偏好
  • 这种关系更为自然,而且在
    偏好
    煎饼
    /
    冰淇淋
    之间可以灵活切换。因为
    用户
    可以喜欢许多
    煎饼
    冰尖叫
    作为他的首选。顺便说一句,
    Icescream
    Pancake
    可能会受到许多用户的喜爱
然后,模型的定义是明确的:

class User < ActiveRecord::Base
  has_many :preferences
end

class Preference < ActiveRecord::Base
  has_many :preference_pancakes
  has_many :preference_icescreams

  has_many :pancakes, through: :preference_pancakes
  has_many :icescreams, through: :preference_icescreams
end

class Pancake < ActiveRecord::Base
  has_many :preference_pancakes
end

class Icecream < ActiveRecord::Base
  has_many :preference_icescreams
end

class PreferencePancake < ActiveRecord::Base
  belongs_to :preference
  belongs_to :pancake
end

class PreferenceIcecream < ActiveRecord::Base
  belongs_to :preference
  belongs_to :icescream
end
class用户
首先,我想我们可以这样修改模式:

  • 用户
    有许多
    偏好
  • Preference
    通过
    PreferencePancake
    有许多
    Pancake
    ,因此
    PreferencePancake
    Preference
    Pancake
    的联接表
  • Preference
    通过
    PreferenceIcecream
    有许多
    Icecream
    ,因此
    PreferenceIcecream
    Preference
    Icecream
    的联接表
我为什么建议这样做

  • 一个
    煎饼
    冰淇淋
    是存在的,而不关心
    偏好
  • 这种关系更为自然,而且在
    偏好
    煎饼
    /
    冰淇淋
    之间可以灵活切换。因为
    用户
    可以喜欢许多
    煎饼
    冰尖叫
    作为他的首选。顺便说一句,
    Icescream
    Pancake
    可能会受到许多用户的喜爱
然后,模型的定义是明确的:

class User < ActiveRecord::Base
  has_many :preferences
end

class Preference < ActiveRecord::Base
  has_many :preference_pancakes
  has_many :preference_icescreams

  has_many :pancakes, through: :preference_pancakes
  has_many :icescreams, through: :preference_icescreams
end

class Pancake < ActiveRecord::Base
  has_many :preference_pancakes
end

class Icecream < ActiveRecord::Base
  has_many :preference_icescreams
end

class PreferencePancake < ActiveRecord::Base
  belongs_to :preference
  belongs_to :pancake
end

class PreferenceIcecream < ActiveRecord::Base
  belongs_to :preference
  belongs_to :icescream
end
class用户
谢谢,在这种情况下,我的首选煎饼表只有3个字段,对吗?ID,user\u ID,pancake\u ID,如果我想得到用户喜欢的所有煎饼,我可以做Preferencepancake.where(user\u ID:current\u user)?@martijnkerckhart:Yes,要得到所有
煎饼
您可以查询
pancake.joins(:reference\u pancake)。其中(preference\u pancake: