Ruby on Rails Saturday, March 28, 2015

I have been up all night trying to solve this same problem, and I
finally got it to work.

I'll use your tables to demonstrate what I did::

===================================
class Room < ActiveRecord::Base
belongs_to :roomtype
end

class Roomtype < ActiveRecord::Base
has_many :rooms,
:primary_key => "t_name",
:foreign_key => "type_name"
end
===================================

I tried this oh-so-many different ways, and the solution was as simple
as this. Ideally, I would prefer to set the tables up so that they all
receive the built-in Rails magic, but we have some legacy business logic
where I can only relate some of the data we import (let's say we import
it into "Room") on an old field that's a string datatype.

It's so great to see this working. I am able to write methods in my
model, and use it in web pages.

I hope this helps someone!

--
Posted via http://www.ruby-forum.com/.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/bb2be54297fac237e690047ae6310753%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment