No, that’s no good. I cannot change the schema.
Here’s an idea… would someone let me know how to do this better?
(1.) Make a regular class (not derived from ActiveRecord::Base).
If I do this, will I have to use
ActiveRecord::Base.establish_connection(), or will AR still connect
automatically when it needs to?
(2.) Make functions to give the appearance of an AR model, such as
- Model.new(:col1 => “value1”, :col2 => 2.2222)
- Model#attributes
- Model.find(primary key)
- Model.find([[array],[of],[keys]])
- Model.find(:all, :conditions => “…”, :limit => 12, :offset => 7,
:include => …)
- Model.find(:first, :conditions => [“…?..”, …], :order => “…”,
:joins
=> “…”)
- Model.find_by_sql(“select * from hobos”)
- Model.find_by_sql(“select count(*) from hobos”)
- Model.find_by_some_database_column(…)
- Model.find_all_by_some_database_column(…)
- Model#some_database_column
- Model#some_database_column?
- Model#update_attribute(:column, “value”)
- Model#update_attributes(:col1 => “value1”, :col2 => “value2”)
- Model.update(primary key, :col1 => “value1”, :col2 => “value2”)
- Model.update_all(“price=2*price”, “name=‘the price is right’”)
- Model#save
- Model#save!
- Model#reload
- Model.delete(primary key)
- Model.delete([[array],[of],[keys]])
- Model.delete_all([“price != ?”, @the_price_that_is_right])
- Model#destroy
- Model.destroy_all([“price != ?”, @the_price_that_is_right])
- Model#id # returns primary key
Hmm, well, implementing all of this would be prohibitively difficult
(and I
wouldn’t get proper transaction support, and I don’t know how to
implement
validation and the errors collection, etc.), but I could stick to
implementing just the methods that are really needed for my application.
(3.) Do all database access with SQL. The main question I have here is,
how
can I generate ‘safe’ (escaped) SQL statements from an expression
[“name=?,
age=?”, “ol’ Joe”, 93]?
con = ActiveRecord::Base.connection
con is a subclass of ActiveRecord::ConnectionAdapters::AbstractAdapter
Select:
con.select_all(“SELECT * FROM hobos WHERE poor=FALSE”)
con.select_one(“SELECT * FROM hobos WHERE poor=FALSE”)
con.select_values(“SELECT age,name FROM hobos LIMIT 3”) => [34,25,67]
Create/Update/Delete/Execute:
gee, what would happen if I fed a DELETE to update()?
auto_inc_id = con.insert(“INSERT INTO hobos (name,age) VALUES (‘Old
Joe’,66)”)
num_rows = con.update(“UPDATE hobos SET age=57 WHERE age=56”) #
birthday!
num_rows = con.delete(“DELETE FROM hobos WHERE status=‘dead’”)
con.execute(“ALTER TABLE hobos ADD shopping_cart BOOLEAN”)
Transactions:
con.begin_db_transaction()
con.commit_db_transaction()
con.rollback_db_transaction()
–
View this message in context:
http://www.nabble.com/How-to-fake-composite-primary-keys--t1388565.html#a3797889
Sent from the RubyOnRails Users forum at Nabble.com.