HABTM Help: Via

My current HABTM implementation still works: has_and_belongs_to_many. Notification and privacy are both inherited from Preferences. Preference is STI. I want to try using: has_many ,: through an association.

Model Files

class User < ActiveRecord::Base
  has_and_belongs_to_many :preferences
end

class Preference < ActiveRecord::Base
  has_and_belongs_to_many :users
end

class Notification < Preference
end

class Privacy < Preference
end

Migration files

class UsersHaveAndBelongToManyPreferences < ActiveRecord::Migration
  def self.up
    create_table :preferences_users, :id => false do |t|
      t.references :preference, :user

      t.timestamps
    end
  end

  def self.down
    drop_table :preferences_users
  end
end

class CreatePreferences < ActiveRecord::Migration
  def self.up
    create_table :preferences do |t|
      t.string :title
      t.text :description
      t.string :type

      t.timestamps
    end
  end

  def self.down
    drop_table :preferences
  end
end

class DeviseCreateUsers < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.string :name
      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

I tried the following

class User < ActiveRecord::Base
  has_many :preferences, :through => :preferences_users
end

class Preference < ActiveRecord::Base
  has_many :users, :through => :preferences_users
end

My form looks like

  <% Preference.where(:type => 'Notification').all.each do |notification| %>
    <li>
      <%= check_box_tag 'user[preference_ids][]', notification.id, @user.preference_ids.blank? ? false : @user.preferences.include?(notification) %>
      <%= label_tag notification.description %>
    </li>
  <% end %>

I get

Could not find the association :preferences_users in model User

What am I doing wrong? Is the problem in my form or in my association?

+3
source share
2 answers

Ok, that worked. I needed the PreferencesUser model and there was no has_many: preferences_users on user models and preferences:

class PreferencesUser < ActiveRecord::Base
    belongs_to :user
    belongs_to :preference
end

class User < ActiveRecord::Base
  has_many :preferences_users
  has_many :preferences, :through => :preferences_users
end

class Preference < ActiveRecord::Base
  has_many :preferences_users
  has_many :users, :through => :preferences_users
end

No need to change anything in my form. Hope this helps someone.

+6
source

HABTM N N, , . has_many 1-N .

:

  • has_many: ( )
  • :
  • user_id

: through, , , , , .

- N-N, , :

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User"
end

class User < ActiveRecord::Base
  has_many :friends, :through => :friendships, :conditions => "status = 'accepted'"
  has_many :requested_friends, :through => :friendships, :source => :friend, :conditions => "status = 'requested'", :order => :created_at
  has_many :pending_friends, :through => :friendships, :source => :friend, :conditions => "status = 'pending'", :order => :created_at
end 

, :

#in you User model
has_many :notifications, :source => :preferences
has_many :privacies, :source => :preferences

#in your Preference model
belongs_to :user
+1

Source: https://habr.com/ru/post/1782436/


All Articles