Hi, I’m a novice and could use help with a join table question. I have
a join table “galleries_questions”.
I can’t figure out how to query it for pictures in a selected gallery.
Controller:
@gallery = Gallery.where(“minisection_id” =>
session[:minisection_id]).first
@pictures = Gallery.find(@gallery).pictures
View:
<% @pictures.each do |picture| %>
<%= image_tag picture.image_url.to_s %>
ERROR:
PGError: ERROR: column pictures.gallery_id does not exist
LINE 1: SELECT “pictures”.* FROM “pictures” WHERE “pictures”."galle…
^
: SELECT “pictures”.* FROM “pictures” WHERE “pictures”.“gallery_id” =
11
Any suggestions?
Thanks
Yes, its actually ‘galleries_pictures’
class Gallery < ActiveRecord::Base
has_and_belongs_to_many :pictures
class Picture < ActiveRecord::Base
has_and_belongs_to_many :galleries
schema:
create_table “galleries_pictures”, :id => false, :force => true do |t|
t.integer “gallery_id”
t.integer “picture_id”
end
“Not relevant, but @gallery.pictures would work there…” Where?
@pictures = @gallery.pictures results in the following:
PGError: ERROR: column pictures.gallery_id does not exist
LINE 1: SELECT “pictures”.* FROM “pictures” WHERE “pictures”."galle…
^
: SELECT “pictures”.* FROM “pictures” WHERE “pictures”.“gallery_id” =
11
On Thu, May 2, 2013 at 1:00 PM, Dave C. [email protected]
wrote:
I have a join table “galleries_questions”.
I’ll hope that’s really ‘galleries_pictures’ 
I can’t figure out how to query it for pictures in a selected gallery.
How do you have the relationship defined in the models?
Controller:
@gallery = Gallery.where(“minisection_id” =>
session[:minisection_id]).first
@pictures = Gallery.find(@gallery).pictures
Not relevant, but @gallery.pictures would work there…
–
Hassan S. ------------------------ [email protected]
twitter: @hassan
On Thu, May 2, 2013 at 6:07 PM, Dave C. [email protected]
wrote:
If you don’t top-post, this will be easier 
class Gallery < ActiveRecord::Base
has_and_belongs_to_many :pictures
class Picture < ActiveRecord::Base
has_and_belongs_to_many :galleries
What does the migration for the creation of your join table look like?
“Not relevant, but @gallery.pictures would work there…” Where?
replacing ‘@pictures = Gallery.find(@gallery).pictures’
–
Hassan S. ------------------------ [email protected]
twitter: @hassan