Rails Join 2 table

Hi,

I’ve 2 tables

  1. Users
  2. 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

I’m using rails 3.1

Thank you,
Sayuj

On 8 August 2011 09:55, Sayuj O. [email protected] wrote:

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.

Colin

On 8 August 2011 10:36, Sayuj O. [email protected] wrote:

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.

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?

Colin

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

What I need to do to fix this?

On Mon, Aug 8, 2011 at 3:19 PM, Colin L. [email protected]
wrote:

It results:
around it) it might be easier to suggest a solution.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

Setup accessible (or protected) attributes for your model

attr_accessible :email, :password, :password_confirmation,
:remember_me

def role?(role)

return !!self.roles.find_by_name(role.to_s.camelize)

end

end

class StatusMessage < ActiveRecord::Base
belongs_to :users
#default_scope :order => “created_at DESC”
end

CONTROLLER:

class StatusMessageController < ApplicationController
def index
@status = StatusMessage.all
end

def create
@status = StatusMessage.new
@status.user_id = current_user.id
@status.status = params[:status]
@status.save
redirect_to :action => “index”
end

end

ERB:

Home

<%= form_tag do |f| %> <%= text_area_tag :status %> <%= submit_tag "Submit" %> <% end %>
*<% @status.each do |s| %> <%= s.users.email %> <%= s.status %>
<% end %>*

ERROR MSG:

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

Extracted source (around line #9):

6: <% end %>
7:

8: <% @status.each do |s| %>
9: <%= s.users.email %>
10: <%= s.status %>

11: <% end %>

On Mon, Aug 8, 2011 at 4:06 PM, Colin L. [email protected]
wrote:

:token_authenticatable, :encryptable, :confirmable, :lockable,

end

redirect_to :action => "index"

<%= submit_tag “Submit” %>
You received this message because you are subscribed to the Google G.
“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

Colin,
When I use s.user.email, it shows:

NoMethodError in Status_message#index

Showing *
/home/sayuj/work/sayuj/microblog/app/views/status_message/index.html.erb*where
line
#9 raised:

undefined method `user’ for #StatusMessage:0xb126384

Extracted source (around line #9):

6: <% end %>
7:

8: <% @status.each do |s| %>
9: <%= s.user.email %>
10: <%= s.status %>

11: <% end %>

On 8 August 2011 11:40, Sayuj O. [email protected] wrote:

On Mon, Aug 8, 2011 at 4:06 PM, Colin L. [email protected] wrote:

On 8 August 2011 11:17, Sayuj O. [email protected] wrote:

[…]
class StatusMessage < ActiveRecord::Base
belongs_to :users

That should be :user, singular.

Did you make this correction as I suggested previously, so it should
be belongs_to :user (each message belongs to one user so singular)

Colin

On 8 August 2011 11:17, Sayuj O. [email protected] wrote:

:timeoutable and :omniauthable

class StatusMessage < ActiveRecord::Base
belongs_to :users

That should be :user, singular.

def create
ERB:

Home

<%= form_tag do |f| %> <%= text_area_tag :status %> <%= submit_tag "Submit" %> <% end %>
<% @status.each do |s| %> <%= s.users.email %>

That should be s.user.email. Each message has just one user.

Colin

On Mon, Aug 8, 2011 at 4:42 PM, Colin L. [email protected]
wrote:

That should be :user, singular.

Did you make this correction as I suggested previously, so it should
be belongs_to :user (each message belongs to one user so singular)

Colin

Yes I made the changes.
Error is “undefined method `user’ for #StatusMessage:0xb126384

On 8 August 2011 12:34, Sayuj O. [email protected] wrote:

On 8 August 2011 11:17, Sayuj O. [email protected] wrote:

Yes I made the changes.
Error is “undefined method `user’ for #StatusMessage:0xb126384

Could you post the code for the StatusMessage class again please?

Colin

On Tue, Aug 9, 2011 at 11:08 AM, Sayuj O.
[email protected]wrote:

On 8 August 2011 11:40, Sayuj O. [email protected]

belongs_to :users
Error is “undefined method `user’ for #StatusMessage:0xb126384

Could you post the code for the StatusMessage class again please?

class StatusMessage < ActiveRecord::Base
belongs_to :users
default_scope :order => “created_at DESC”
end

I should use *belongs_to :user *instead of belongs_to :users

Thank you Colin.

On 9 August 2011 07:00, Sayuj O. [email protected] wrote:

On 8 August 2011 11:17, Sayuj O. [email protected]
Colin
end

I should use belongs_to :user instead of belongs_to :users

In my initial post I pointed out two errors. That was one of them.
Then later I queried whether you had made that correction and you said
you had.

Colin

On Mon, Aug 8, 2011 at 5:54 PM, Colin L. [email protected]
wrote:

wrote:
be belongs_to :user (each message belongs to one user so singular)

Colin

Yes I made the changes.
Error is “undefined method `user’ for #StatusMessage:0xb126384

Could you post the code for the StatusMessage class again please?

class StatusMessage < ActiveRecord::Base
belongs_to :users
default_scope :order => “created_at DESC”
end

Colin