i followed doctor nic's tutorial on composite primary key but it didn't
work for me, i'm not sure why, could someone take a look please?
i added the following to the bottom of my environment.rb file
==> require 'composite_primary_keys'
require 'rubygems' #am i supposed to put this in programme.rb
require 'composite_primary_keys' #or 003_create_programmes.rb?
#at the moment, it is placed in the 003_create_programmes.rb
class CreateProgrammes < ActiveRecord::Migration
def self.up
create_table :programmes do |t|
t.column :course_code, :string, :null => false
t.column :programme_code, :string, :null => false
t.column :type, :string
t.column :last_updated, :timestamp, :null => false
end
end
def self.down
drop_table :programmes
end
end
after i scaffolded programme, in the add new programme page
i do not see the input textfield for [type]
and the composite keys doesn't work..
anyone knows whats wrong?
on 2007-11-13 04:28
on 2007-11-13 04:33
i've also tried placing the > require 'rubygems' > require 'composite_primary_keys' in programmes.rb, didn't work either
on 2007-11-14 02:05
Well, I don't have an actual answer for you. But.....
Why, oh why, are you using composite primary keys if you're creating
the table? I can certainly see having the need with legacy databases,
where you have no control over the design. However, given that you are
showing a migration in your original post, then why are you making
your life unnecessarily complicated?
Why not just give Rails its simple integer primary key? Rails will be
happier, you will be happier, in the long run, and all will be right
with the world.
Now don't get me wrong, there may be a perfectly reasonable need in
your case. I don't have all the facts here. But, as many on this list
have said before, "Fight Rails and it will fight back."
I don't find this unique to Rails. I see this with just about any
object-relational mapping systems. It's usually best to have a simple
identifier that can be managed entirely by the framework whenever
possible. Rows in a database become object instances. Having that one-
to-one, object-to-row identity just makes things run much smoother.
All that aside, looking at your programmes table it sure looks to me
like a perfectly resaonable join between courses and programes. Why,
do you need a composite_primary_keys plugin for this anyway? Why not
just let Rails handle that relationship for you using a has_many?
Course
has_many :programmes
has_many :somethings :through => :programmes <-- ??? wait a
second, is this many-to-many or one-to-many?
--- one-to-many ---
Course
has_many :programmes
Programme
has_one :course
--- migration ---
class CreateProgrammes < ActiveRecord::Migration
def self.up
create_table :programmes do |t|
t.column :programme_code, :string, :null => false << --- Not
the table's PK but a unique programme code column
t.column :course_id, :integer
t.column :type, :string
t.column :last_updated, :timestamp, :null => false
end
add_index :programmes, :programme_code, :unique => true
end
def self.down
drop_table :programmes
end
end
Note: This table will have an id column since you didn't tell Rails
not to create one.
--- Before Save ---
Then write a before_save method to generate a user presentable
programme_code for display in your interface.
Again, I'm speculating a lot here. I just wanted to make sure you're
thinking about this the right way.
On Nov 12, 10:33 pm, Jojo Mojo <rails-mailing-l...@andreas-s.net>
on 2007-11-14 02:32
at the moment, the composite key is 100% necessary in order for my application to move on, thanks for the advice still
on 2007-11-14 03:28
Is this due to an outside influence? Outside the Rails application that is? Just curious, because I see no technical reason requiring a composite key. Obviously it's your application and I wish you luck in finding a solution. On Nov 13, 8:32 pm, Jojo Mojo <rails-mailing-l...@andreas-s.net>
on 2007-11-21 06:09
Robert Walker wrote: > Is this due to an outside influence? Outside the Rails application > that is? Just curious, because I see no technical reason requiring a > composite key. Obviously it's your application and I wish you luck in > finding a solution. > > On Nov 13, 8:32 pm, Jojo Mojo <rails-mailing-l...@andreas-s.net> its just that the combination of course_code and programme_code must be unique for example: course_code: cou101 programme_code: bahs in the database: cou101, bahs course_code: cou101 programme_code:jahs in the database: cou101, jahs i'm not sure how a has_many can solve this problem
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.