【问题标题】:Need help to build correct associations between models需要帮助来建立模型之间的正确关联
【发布时间】:2020-03-06 18:35:15
【问题描述】:

我有两个模型:UserChannel。有4种不同价格的频道:1000的动物频道,800的新闻频道等。每个用户可以订阅不同的频道,所以最终,我创建了三个具有以下关联的模型:

Channel
has_many :user_channels
has_many :users, through: :user_channels

User 
has_many :user_channels
has_many :channels, through: :user_channels

UserChannel
belongs_to :user
belongs_to :channel

但我不确定这是正确的处理方式。

另外令人困惑的是,当我尝试将特定用户的频道价格设为user.user_channels.price 其中priceChannel 的字段时,结果实际上是集合,这对我来说似乎是错误的,但又一次我只是在学习。

架构:

create_table "channels", force: :cascade do |t|
    t.integer "category"
    t.decimal "price"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

create_table "user_channels", force: :cascade do |t|
    t.integer "user_id"
    t.integer "channel_id"
    t.decimal "spent_amount"
    t.date "start_date"
    t.date "due_date"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

create_table "users", force: :cascade do |t|
    t.string "first_name"
    t.string "last_name"
    t.string "email", default: "", null: false
    t.boolean "admin", default: false
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
  end

【问题讨论】:

  • 那是因为你要求它为所有用户的频道。如果一个用户有很多渠道,而一个渠道有很多用户,你需要指定你要问哪个渠道的价格。您能否编辑您的问题并向我们展示您的架构,以便我查看您定义了哪些列?
  • 感谢您的回复!一定要花点时间
  • @Beartech 我也添加了架构,谢谢!

标签: ruby-on-rails ruby model associations


【解决方案1】:

您没有向 DB 提出正确的问题。如果你想知道一个渠道的价格,仅仅知道用户怎么能告诉你呢?因为UserChannel 是一个关系表,所以你用user.user_channels 询问它“给我这个用​​户的所有频道”。这是一个集合。

@my_channels = user.user_channels
#=> returns an array of UserChannels objects NOT an array of channels 

您必须告诉它您想要哪个频道的价格。有很多方法可以做到这一点:

@my_var = user.channels.where(category: 'animals')

这将为您提供符合该标准的任何内容的数组。如果类别列具有唯一约束,它只会给您一个结果,但仍会位于数组中。

@my_var = user.channels.find_by(category: 'animals') 

将调用该查询并给出作为#<channel> 对象返回的第一项。然后您可以查询以下商品的价格:

@my_var.price #or all in one:
@my_var = user.channels.find_by(category: 'animals').price

您不需要直接访问user_channels。我通常会将其命名为 users_channels 以表示 has_many_through 的双向性质,因为您可以执行以下操作:

@channel = Channel.first
@channel.users 
#=> returns an array of all users that have that channel.

【讨论】:

  • 我会进入 Rails 控制台并玩转关联,直到您对它们有所了解。进行查询时,请查看返回的内容。是单个对象,还是对象数组等等。
  • 您无需加入即可获取数字并求和。 pluck 是一个很好的了解方法。您可以执行@some_channel.user.pluck(:spent_amount),它会返回这些金额的数组。这意味着您可以执行任何Array 操作,例如:'@some_channel.user.pluck(:spent_amount).sum` 并获得总和。
  • 我真的鼓励您阅读该链接。这非常有帮助。可用的方法很多。因为sum 是一个SQL 函数,所以他们为它创建了一个方法。而不是我的.pluck 示例,您可以这样做:@some_channel.users.sum(:spent_amount) -note 我在前面评论的那个 pluck 示例中将user 设为单数,这是一个错字。
  • 并不是让你不知所措,但由于 Rails 喜欢 hash_with_indifferent_access,值得注意的是它可以是:@some_channel.users.sum(:spent_amount)@some_channel.users.sum("spent_amount"),Rails 可以识别字符串或符号相同.
【解决方案2】:

另外令人困惑的是,当我尝试以 user.user_channels.price 的形式获取特定用户的频道价格时,其中价格是频道的字段,结果实际上是集合,这对我来说似乎是错误的,但我又一次只有学习。 如果您想知道特定用户在频道上的价格,您需要这样做

# Assume you know ids of user and channel both. user = User.find(1).includes(:channels) #Preload channeles to look through them finding seeking channel and its price channel_id = 1 price = user.channels.find_by(id: channel_id)&.price # User might not be subscribed to the channel. In that scenario the price will be nil.

【讨论】:

  • 另外,不需要实际查看 user_channels,因为通道价格不依赖于 user_channels 表,因为它存储在通道本身中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多