StatusMessages @user = Users.find(1) @status = @user.status_messages
with these lines I’m getting the record of users and status_messages table. here I can refer in @status with @user.id for status messages of each user. in this I want to check this for each user in my code.
is there any way to get the data - username(from Users) and status_message(from StatusMessage) in a single variable.
ie I want to access like: @userstatus.username @userstatus.status_message
If @status_message is a StatusMessage then you can say @status_message.user.name @status_message.message
or whatever the column names are. This assumes that StatusMessage
belongs_to :user
Does that solve the problem?
If you don't want to use @status_message.user.name but would rather
say @status_message.user_name then define an instance method of
StatusMessage that returns user.name. In either case don't forget to
check for @status_message.user nil if you can ever have a message
without a user.
Please don't top post, it makes it difficult to follow the thread.
Insert your reply at appropriate points in the previous message.
Thanks.
If you could manage to post in plain text rather than html it would
also make life more pleasant for some of us.
Hi Colin,
It results:
NoMethodError in Status_message#index
Showing
/home/sayuj/work/sayuj/microblog/app/views/status_message/index.html.erb
where line #9 raised:
undefined method `unscoped' for Users:Module
Perhaps if you showed us the line causing the problem (and a few lines
around it) it might be easier to suggest a solution.
Also show the model class definitions for user and statusmessage.
Copy and paste the code, do not retype it.
Also tell us anything unusual about your setup. Are you using an
authorisation gem for example?
Please see my code. I am using devise as authentication controller.
MODELS:
class User < ActiveRecord::Base #has_and_belongs_to_many :roles
has_many :status_messages
Include default devise modules. Others available are:
:token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable