How come I allways immediately run into difficult stuff when I’m trying
some new programming language? Am I blind for the simplicity of
Ruby/Rail, which a see must be there? Anyway, don’t try and answer this
rhetoric question. I have got another one for you, seemingly difficult.
I’m struggling with a n:m relationship (in a database, that is) and its
join table.
Here are my objects:
class Painting
has_and_belongs_to_many :themes
which maps to db entity
painting
id
class Theme
has_and_belongs_to_many :paintings
which maps to db entity
theme
id
And of course the join table
paintings_themes
painting_id
theme_id
So, paintings have one or more themes, and a theme can belong to more
paintings.
In my ExpositionController, which shows the paintings, I have:
@painting_pages, @paintings = paginate :paintings, :per_page => 10
But now I want only to show paintings with a certain theme (still
paginated of course).
So I have to put a :condition in the paginating code. I’ve tried
something like:
@painting_pages, @paintings = paginate :paintings, :per_page => 10,
:conditions => [‘displayable=? AND themes[0].name=?’, true, ‘flowers’]
which shows only displayable paintings with theme=flowers, but the
themes[0].name part doesn’t compile.
I’m stuck. My mind simple goes blank on this theme (pardon the confusing
last term).
Anyone who can help me out?
Thanks.