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

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

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?

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?

Colin

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: <br />
8: <% @status.each do |s| %>
9: <%= s.users.email %>
10: <%= s.status %> <br />
11: <% end %>

[...] 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

That should be :user, singular.

#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:

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

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

Colin

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: <br />
8: <% @status.each do |s| %>
9: <%= s.user.email %>
10: <%= s.status %> <br />
11: <% end %>

> [...] > 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

[…]

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

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

[…]

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

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

[…]

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

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

I should use belongs_to :user instead of belongs_to :users

Thank you Colin.

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