Belongs_:to association

class Shopcategory0 < ActiveRecord::Base
set_table_name “shopcategories_0”

has_many :shopcategory1s, :foreign_key => :parentcategory_id
end

class Shopcategory1 < ActiveRecord::Base
set_table_name “shopcategories_1”

belongs_to :shopcategory0, :foreign_key => :shopcategory_id
end

class Shopcategory0sController < ApplicationController
def show
@shopcategory = Shopcategory0.all (:joins
=> :shopcategory1s)
end
end


That’s the SQL result:

Shopcategory0 Load (0.1ms) SELECT shopcategories_0.* FROM
shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.id


And this is what I search (notice the end):

Shopcategory0 Load (0.1ms) SELECT shopcategories_0.* FROM
shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.shopcategory_id

I suppose there is a way to do that…I’ll appreciate your help !!!

On Jul 28, 10:34 am, cocozz [email protected] wrote:

end

shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.shopcategory_id

I suppose there is a way to do that…I’ll appreciate your help !!!

Is there a question in there?

-e

Of course !

Look at the end:

"
class Shopcategory0 < ActiveRecord::Base
set_table_name “shopcategories_0”

has_many :shopcategory1s, :foreign_key => :parentcategory_id
end

class Shopcategory1 < ActiveRecord::Base
set_table_name “shopcategories_1”

belongs_to :shopcategory0, :foreign_key => :shopcategory_id
end

class Shopcategory0sController < ApplicationController
def show
@shopcategory = Shopcategory0.all (:joins
=> :shopcategory1s)
end
end


That’s the SQL result:

Shopcategory0 Load (0.1ms) SELECT shopcategories_0.* FROM
shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.id


And this is what I search (notice the end):

Shopcategory0 Load (0.1ms) SELECT shopcategories_0.* FROM
shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.shopcategory_id
"

According to you, you are stating that…

shopcategories_0 table has both and id column and a shopcategory_id
column.

You want to reference a foreign key through that table…

You probably need to use…

class Shopcategory1 < ActiveRecord::Base
set_table_name “shopcategories_1”

belongs_to :shopcategory0, :class => :shopcategory0, :foreign_key =>
:shopcategory_id
end

You should really use the class for the model that houses the
shopcategory_id. If that model is the :shopcategory0 then it appears
you aren’t normalizing your tables properly and you might want to
rethink your design a bit. Also, your table names are very confusing.

2009/7/28 cocozz [email protected]:

end
=> Â :shopcategory1s)

On Jul 28, 10:34Â am, cocozz [email protected] wrote:

 set_table_name “shopcategories_1”


Shopcategory0 Load (0.1ms) Â SELECT shopcategories_0.* FROM
shopcategories_0 INNER JOIN shopcategories_1 ON
shopcategories_1.parentcategory_id = shopcategories_0.shopcategory_id

I suppose there is a way to do that…I’ll appreciate your help !!!

Is there a question in there?

I too have difficulty seeing what is the question you are asking. Do
you mean you are trying to generate that particular query? What does
‘notice the end’ mean? If so why are you trying to generate some
particular sql. Generally you should think about what you want to
achieve at a higher level and let Rails worry about the query.

Colin

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs