Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #trying to be able to get the message author in the view easily.
- class Message < ActiveRecord::Base
- has_many :msg_owners
- has_many :users, :through => :msg_owners
- has_one :author, :source => :user, :through => :msg_owners, :conditions => ["users.author = ?",true]
- end
- class User < ActiveRecord::Base
- has_many :msg_owners
- has_many :messages, :through => :msg_owners
- end
- #this table has a record for everyone who can see the message. The boolean author field is true for the person that originated the message
- class MsgOwners < ActiveRecord::Base
- belongs_to :user
- belongs_to :message
- end
- index controller
- def create
- @message = Message.new(params[:message])
- respond_to do |format|
- if @message.save
- #add a record in the MsgOwners table that designates the author. Any recipients would also get a record but boolean would be false.
- @msg_owner = MsgOwners.new(:user_id => current_user.id, :message_id => @message.id, :author => true)
- @msg_owner.save
- flash[:notice] = 'Message was successfully created.'
- @messages = Message.find(:all, :order => "created_at DESC")
- format.html { render :action => "index" }
- format.xml { render :xml => @message, :status => :created, :location => @message }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @message.errors, :status => :unprocessable_entity }
- end
- end
- end
- index view
- <% @messages.each do |message| %>
- <%=h trying to get author's name here %> said "<%=h message.message %>"<br/>
- <%=h message.time_ago_or_time_stamp %>
- <% end %>
Add Comment
Please, Sign In to add comment