Topic: Find over 3 models by this modelstructure

Hello together,

i' m very desperate. I have asked this question also in stackoverflow, but i don't get the right tipp there
http://stackoverflow.com/questions/1055 … over-the-3
Now i hope you could help me. This is my modelstructure

class Model1 < ActiveRecord::Base
  has_many :model2s
end
class Model2 < ActiveRecord::Base
  belongs_to :model1s
  has_many :model3s
end
class Model3 < ActiveRecord::Base
  belongs_to :model2s
end

In the table of Model1 exists id.

In the table of Model2 exists id and model1_id.

In the table of Model3 exists id and model2_id.

I want to get data out of Controller of the model3.

class Model3Controller < ApplicationController
  def create
    @model2 = Model2.find(params[:model3controller][:model2_id])
    @model1 = model3.find_by_id(params[:model3controller][:id]).model2.model1
  end
end
@model2controller works very fine, but the solution of model1 doesnt work.

I know that the model2 and model1 is conntect through the belongs_to and has_many construct.

The main question is. How get i data of model1 out of controller of model3?
Any help are very welcome and know i also in forum active. what a great feeling

Re: Find over 3 models by this modelstructure

those params are just wrong! Where on earth are they coming from? but I'll take you at your word that they work (Must have given you a real headache setting up such a weird params hash)

So assuming that the following as you say, dies actually work

 @model2 = Model2.find(params[:model3controller][:model2_id])

Then

 @model2 = Model2.find(params[:model3controller][:model2_id])
 @model1 = @model2.model1

Will get you what you want.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)