1

I'm building a sinatra app with Active record. The idea is to essentially have a custom email app. Here I have the models User and Message. A User has_many :messages and a Message belongs_to :user. This may be where I have the issue. I also have it set up for a Message belongs_to :user and has_many :users. here are the models

Now when I create a message in the action controller I am attempting to use the shove methods to put the new message in a user's messages array. If I attempt to "share" this message with multiple users at once with all the user's id's in params( @user = User.find(id) and then user.messages << @new_message) the last user will have the message stored in it's .messages array. However only the last one to be iterated.

class Message < ActiveRecord::Base
   belongs_to :user 
   has_many :users
end  
class User < ActiveRecord::Base
    validates :username, presence: true, uniqueness: true
    has_secure_password
    has_many :messages  
end

The idea is the writer "owns" the message but can share it with many users. Here are the tables

class CreateUsers < ActiveRecord::Migration
  def change 
    create_table :users do |t|
      t.string :username 
      t.string :email 
      t.string :password_digest
    end
  end
end
class CreateMessages < ActiveRecord::Migration
  def change 
    create_table :messages do |t| 
          t.string :message 
           t.string :author
           t.integer :user_id 
           t.integer :user_ids
           t.integer :share_id
           t.string :title 
           t.timestamps
  end
end 
end
# action controller 
 new_params = {}
      new_params[:message] = params["message"]
      new_params[:title] = params["title"]
      new_params[:author] = params["author"]
      new_params[:user_id] = params["user_id"] 
      @message = Message.create(new_params)
      # @share = Share.create 
      # @message.share_id = @share.id
      response.map do  |x| 

        x.messages << @message 
        x.save!
      end 
      @all = User.all 

      @user = User.find_by(username: @message.author) 
      erb :"/user/sent" 

I am fairly sure this is because my associations are not set up properly.

mrzasa
  • 22,895
  • 11
  • 56
  • 94
rTaylor
  • 39
  • 5

1 Answers1

0

You need to have has-and-belongs-to-many relationship between users and messages to implement sharing multiple messages to multiple users. Create an additional record, e.g. MessageShare and do has many to it from both sides:

class MessageShare < ActiveRecord::Base
  belongs_to :user
  belongs_to :message
end

class Message < ActiveRecord::Base
  has_many :message_shares
end

class User < ActiveRecord::Base
  has_many :message_shares
end

message_shares table should have user_id and message_id integer columns.

mrzasa
  • 22,895
  • 11
  • 56
  • 94
  • You look like a master at this. I'm not sure how to implement this. I attempted this and did not get it to work either. – rTaylor Nov 18 '19 at 17:01
  • I have a share, message, and user model. I've attempted to add a few multiple user.ids to the share.user_ids in the action controller. None of these attempts are works. I'm not really sure how to access the users and messages. Before I know that has_many :messages gave the user the user.messages methods. – rTaylor Nov 19 '19 at 06:59
  • See https://www.sitepoint.com/master-many-to-many-associations-with-activerecord/ and https://stackoverflow.com/questions/5120703/creating-a-many-to-many-relationship-in-rails/5120734. If it doesn't help, come back with new questions :) – mrzasa Nov 19 '19 at 08:24