Schema.rb naming conventions

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.

C:\code\strawr\db>
C:\code\strawr\db>
C:\code\strawr\db>type schema.rb

This file is autogenerated. Instead of editing this file, please use

the

migrations feature of ActiveRecord to incrementally modify your

database, and

then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 3) do

create_table “categories”, :force => true do |t|
t.column “category”, :string, :default => “NULL”
end

create_table “category_feeds”, :force => true do |t|
t.column “category_id”, :integer, :default => 0
t.column “feed_id”, :integer, :default => 0
end

create_table “feeds”, :force => true do |t|
t.column “feed”, :string, :default => “NULL”
end

end

C:\code\strawr\db>

thanks,

Thufir

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 Dec 14, 2007 2:06 PM, Thufir [email protected] wrote:

a migration command was the correct way to create the schema.
database, and
t.column “feed_id”, :integer, :default => 0

thanks,

Thufir


Ryan B.
http://www.frozenplague.net

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.

Going by http://grover.open2space.com/node/138, the corresponding
model
would CustomerOrder, is that right?

thanks,

Thufir

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

<http://api.rubyonrails.com/classes/ActiveRecord/Associations/
ClassMethods.html>

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.

-Thufir

On Dec 14, 1:51 am, Thufir [email protected] wrote:

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.

| Privacy Policy | Terms of Service | Remote Ruby Jobs