1

I have 4 models with complex relations. 3 of them should have descriptions, that should be enable only for user who's create. In other words every user has his own description for Group (for example), or for Post, o something else. Let's talk about only one model, because others are very same. What I have: user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, omniauth_providers: [:vkontakte]
  has_and_belongs_to_many :groups
  has_many :descriptions
end

group.rb

class Group < ActiveRecord::Base
  has_and_belongs_to_many :users
  has_many :descriptions, :as => :describable
  accepts_nested_attributes_for :descriptions
end

description.rb

class Description < ActiveRecord::Base
  belongs_to :user
  belongs_to :describable, :polymorphic => true
end

table for descriptions

create_table "descriptions", force: :cascade do |t|
    t.integer  "user_id" -- belongs_to
    t.string   "content"
    t.integer  "describable_id"
    t.string   "describable_type"
  end

How to display the description for group that belongs to current_user (I use devise)? How to build an update form with nested description?

I try to do it, but it's not work. I've ask question about part of problem here.

Community
  • 1
  • 1
nobilik
  • 736
  • 9
  • 29
  • You're missing part of the polymorphic association in the User model, it should be `has_many :descriptions, as: :describable` (you have it correctly in the Group model) – Roma149 Dec 12 '15 at 10:12
  • @Roma149 User is not describable model. It's just has many descriptions with has_many - belongs_to. And this descriptions describes models group, post, etc. – nobilik Dec 12 '15 at 10:38
  • Ok, but there's no `belongs_to :user` in the `Description` model, so I guess you'd get an error if you tried to use that association. – Roma149 Dec 13 '15 at 01:47
  • @Roma149 thanks, fixed – nobilik Dec 13 '15 at 08:37

1 Answers1

0

Why do you have an extra model called description?

Although it's not a problem in itself, you really don't need to have a model just for description.

--

Profile

Instead, you may wish to put the details into a profile model, or simply in the user model (there's nothing wrong with adding extra attributes to a Devise model).

We use a profile model, which gives us the ability to add as many "extra" fields as we want to the user model:

enter image description here

You can set it up like this:

#app/models/user.rb
class User < ActiveRecord::Base
   has_one :profile
   accepts_nested_attributes_for :profile
   before_create :build_profile

   delegate :description, :name, to: :profile, prefix: false #-> @user.description
end

#app/models/profile.rb
class Profile < ActiveRecord::Base
   belongs_to :user
end

This will allow you to create a single profile per user, have that profile built when the user is created, and then change as many options inside the profile as you wish.

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • @Rich_Peck My English is not so good, sorry. I've update my question with some more explanations. So any user can make description for group, board, post, etc. It will be something like a private note for record of any of types. And I need that to be visible only for its creator/owner. And you answer to me how to add description to User object, if I correctly understand you. – nobilik Dec 12 '15 at 09:31
  • Yes I answered what I thought you were asking. If a user can create a description for a group/board etc, then it will be a totally different answer. I will update in a minute – Richard Peck Dec 12 '15 at 09:34