# Relationships in models

**URL:** <https://www.ruby-forum.com/t/relationships-in-models/160786>\
**Category:** Rails\
**Created:** [February 19, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/relationships-in-models/160786 "2009-02-19T17:38:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [February 19, 2009, 5:38pm UTC](https://www.ruby-forum.com/t/relationships-in-models/160786/1 "2009-02-19T17:38:48Z")

</div>

I am new to rails and have got very confused with the above. It was  
working and then I made changes and it has gone downhill from there.  
And I was not taking regular backups…  
I have moved slightly outside of the conventions but it is not unknown  
for a record type to be refered in another record in several fields -  
users being a good example.  
My schema file.  
create\_table “messages”, :force =\> true do |t|  
t.integer “from\_user”, :null =\> false  
t.integer “to\_user”, :null =\> false  
t.text “message”  
t.datetime “created\_at”  
t.datetime “updated\_at”  
end

add\_index “messages”, [“from\_user”], :name =\>  
“fk\_messages\_from\_users”  
add\_index “messages”, [“to\_user”], :name =\> “fk\_messages\_to\_users”

create\_table “users”, :force =\> true do |t|  
t.string “user\_id”  
t.string “first\_name”  
t.string “surname”  
etc etc

My model files  
class Message \< ActiveRecord::Base  
belongs\_to :user\_from,  
:class\_name =\> “User”,  
:foreign\_key =\> “from\_user”

```
 belongs_to :user_to,
          :class_name => "User",
          :foreign_key => "to_user"

          validates_presence_of :message

```

end  
class User \< ActiveRecord::Base  
has\_many :user\_from,  
:class\_name =\> “Message”,  
:foreign\_key =\> “from\_user”

```
 has_many :user_to,
          :class_name => "Message",
          :foreign_key => "to_user"

```

etc etc  
I made changes to user looking at various google results. Previous I  
simply had  
class User \< ActiveRecord::Base  
has\_many :messages  
etc etc

The controller action.  
def my\_messages  
user = User.find(session[:userid])  
@first\_name = user.first\_name  
@messages = Message.paginate(:page =\> params[:page], :per\_page =\> 10,  
:conditions =\> [“from\_user = ? or to\_user = ?”,  
session[:userid],  
session[:userid]],  
:order =\> “created\_at DESC”)  
@message = Message.new(params[:message])  
@message.from\_user = user  
@message.to\_user = User.find(1)  
if request.post? and @message.save  
@message = Message.new()  
@messages = Message.paginate(:page =\> params[:page], :per\_page =\>  
10,  
:conditions =\> [“from\_user = ? or to\_user = ?”, session  
[:userid], session[:userid]],  
:order =\> “created\_at DESC”)  
end

And this is where I get confused. if I change the two lines above  
@message.from\_user = user  
@message.to\_user = User.find(1) # I know its hard coded but that  
is the least of my worries  
to  
@message.from\_user = session[:userid]  
@message.to\_user = 1  
then I get expected results (ie value is 6) but  
message.from\_user.first\_name will give an undefined method error.  
If I use the former code then the value of from\_user is 1 rather than  
6. I have not a clue where it gets 1 from.

So, in summary, I have not got this. Any help appreciated.

Regards,

john

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [February 19, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/relationships-in-models/160786/2 "2009-02-19T22:05:05Z")

</div>

Thank you for this. I agree with what you have said but could you give  
an example of what you mean. For instance, are my model files correct?  
Also the point I made about setting message.user\_from to user then  
gives a value of 1 rather than a value of 6 (the id of the user).

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [February 19, 2009, 6:55pm UTC](https://www.ruby-forum.com/t/relationships-in-models/160786/3 "2009-02-19T18:55:31Z")

</div>

> message.from\_user.first\_name will give an undefined method error.

message.from\_user is an integer

message.user\_from is a reference to a user, and user has a first\_name  
method

---

<div class="post-metadata">

**Author:** ![norricorp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/df705f/32.png) [@norricorp](https://www.ruby-forum.com/u/norricorp)\
**Post date:** [February 20, 2009, 7:52pm UTC](https://www.ruby-forum.com/t/relationships-in-models/160786/4 "2009-02-20T19:52:30Z")

</div>

Now I get it.  
The association name is the name used for the object. And I had the  
field name and the association which I had not realised but a least it  
emphasises the point.  
And by assigning the object to the this, I can then display values for  
the referred object. So my code is now  
class Message \< ActiveRecord::Base  
belongs\_to :user\_from\_obj,  
:class\_name =\> “User”,  
:foreign\_key =\> “from\_user”  
etc etc  
user = User.find(session[:userid])  
@message = Message.new(params[:message])  
@message.user\_from\_obj = user  
etc etc  
many thanks .
