У меня есть две модели: User
и Channel
. Существует 4 типа каналов с разными ценами: канал животных на 1000, канал новостей на 800 и др. c. Каждый пользователь может подписаться на разные каналы, поэтому в итоге я создал три модели со следующими ассоциациями:
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
, где price
- это поле Channel
, результат - это коллекция, которая на самом деле мне кажется неправильной, но опять же, я только уч.
схема:
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