0

I guess that I do not know the right terminology yet, thus find it difficult to find the right answer.

So, I have created an engine with Exhibit and Category. I created a third model Categorization so as to assign an exhibit to more than one categories. This has only the exhibit_id and category_id.

What I want to do is to create a page for every category, so I assign an exhibit to the News category, to display it in the "News" page, when to the Photographs category to display it in the "Photographs" page etc. I guess this is a routing configuration, but I have not got there yet (however, please let me know if it is indeed a routing configuration)

My problem is how to retrieve the fields from different models, from only one controller. What I have:

class Categorization < ActiveRecord::Base
  belongs_to :exhibit
  belongs_to :category
end

class Category < ActiveRecord::Base
  has_many :categorizations
  has_many :exhibits, :through => :categorizations
  acts_as_indexed :fields => [:title]
  validates :title, :presence => true, :uniqueness => true 
end

class Exhibit < ActiveRecord::Base
  has_many :categorizations
  has_many :categories, :through => :categorizations, :source => :category
  acts_as_indexed :fields => [:title, :bulb]
  validates :title, :presence => true, :uniqueness => true
  belongs_to :foto, :class_name => 'Image'
end

How do i retrieve the :foto of the Exhibit, which belongs to :category =>"News"?

I tried to add scope :news, where(['category_id="1"']) in Categorization model and i can retrieve Categorization.news but how do I connect the Categorization.exhibit_id with the photo of this exhibit (I guess this is Exhibit.foto)?

I don't know where to start...

Thank you all...

Petros

divibisan
  • 11,659
  • 11
  • 40
  • 58
Cacofonix
  • 465
  • 11
  • 19

1 Answers1

1

I would try this in console:

Categorization.news.first.exhibit.foto    

See if this won't give you the needed Image object. The thing is that news have several possible exhibits.

Alexei Danchenkov
  • 2,011
  • 5
  • 34
  • 49
  • ...exactly! How do i get `all` and not the `first`? In any case, thanks a bunch, the query above was a charm!! – Cacofonix Jul 18 '11 at 22:01
  • I tried this one: `Categorization.news.each do |t|` `t.exhibit.foto` `end` ...but it returned the `Categorization.news` – Cacofonix Jul 18 '11 at 22:13
  • This is strange. I duplicated your code exactly - I get the Image object. It may be that you are looking at console output, which shows you the Categorization.news array (as it should in console). The point is that in the each cycle you are not doing anything with t.exhibit.foto. May be you meant `Categorization.new.each do |t| puts t.exhibit.foto.url end` or something? I don't know if your Image model has any attributes, I assumed url should be part of it, but use what you have there. It should give you your Image attribute. – Alexei Danchenkov Jul 19 '11 at 07:00
  • Have just come back from work and tested it out. You were right, I was trying without the `.url` method. Thanks a bunch for your help was quite enlightening!!! – Cacofonix Jul 19 '11 at 19:07