going by <http://wiki.rubyonrails.org/rails/pages/
ThroughAssociations>, I think that I have my table names in the schema
squared away, but I’m not convinced. For a many-to-many with
ThroughAssociations between tables foo and bar, is the join table
created named foo_bars by convention? If the table name is foo_bars,
then the corresponding model in app/models would be FooBar, is that
right?
If FooBar is created by me, then, going by the comments in schema.rb,
a migration command was the correct way to create the schema.
I’m just not convinced that I’m following convention on this.
The table would be called bars_foos. All join tables are the pluralized
versions of what they’re joining in alphabetical order. Say I have
orders
and customers, the join table will be customers_orders.
You don’t need to post long console feeds (unless we ask for it).
Usually we
can figure out what you want. The longer a post is, the less I feel like
reading it. But that’s just me.
On Fri, 14 Dec 2007 14:11:10 +1030, Ryan B. wrote:
The table would be called bars_foos. All join tables are the pluralized
versions of what they’re joining in alphabetical order. Say I have
orders and customers, the join table will be customers_orders.
On Fri, 14 Dec 2007 14:11:10 +1030, Ryan B. wrote:
The table would be called bars_foos. All join tables are the pluralized
versions of what they’re joining in alphabetical order. Say I have
orders and customers, the join table will be customers_orders.
And I’ve come across this a few different ways:
Many-to-many
There are two ways to build a many-to-many relationship.
The first way uses a has_many association with the :through option and a
join model, so there are two stages of associations.
class Assignment < ActiveRecord::Base
belongs_to :programmer # foreign key - programmer_id
belongs_to :project # foreign key - project_id
end
class Programmer < ActiveRecord::Base
has_many :assignments
has_many :projects, :through => :assignments
end
class Project < ActiveRecord::Base
has_many :assignments
has_many :programmers, :through => :assignments
end
In that example, the name for the join model doesn’t contain the models
which it joins. I also saw a similar naming scheme in the rails recipe
book, I believe. Aaaargh.
The first way uses a has_many association with the :through option and a
join model, so there are two stages of associations.
If you want to use has_many/through, then you should try to come up
with a meaningful name for the relationship. If you are connecting
foos to bars, I would only call the relationship table bars_foos if I
were going to use has_and_belongs_to_many.
Even though it’s now out of fashion, I see no reason not to use habtm
for relationships with no data. If some data appears later, just
rename the table and add some columns.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.