composite-primary-keys / composite_primary_keys Goto Github PK
View Code? Open in Web Editor NEWComposite Primary Keys support for Active Record
Composite Primary Keys support for Active Record
hi, my problem is as title.
I use the plugin in rails 2.3.2 follow the guideline on http://compositekeys.rubyforge.org/
When I start server, it return error message as follow.
uninitialized constant ActiveRecord::Associations (NameError)
This commit changes the "load 'active_record/associations.rb'" back into "require 'active_record/associations.rb'"
this re-introduces the error
"uninitialized constant ActiveRecord::Associations"
Have tried to figure out where the problem is but basically in the callbacks.rb create_with_callbacks method, stepping through the result = create_without_callbacks line jumps directly to the timestamp.rb create_with_timestamps method on a save action.
This is not happening. create_with_timestamps is never called.
Maybe I am only one who is testing CPK with SQLite or maybe I am doing something wrong, but recently I've found the configuration of testing environment for CPK with SQLite very confusing and leading to a faulty situation.
Here's what I did:
[bundle exec] rake sqlite3:build_databases
ended up with ActiveRecord::AdapterNotSpecified
from connection_specification_changes.rb#12test/connections/databases.yml
-- replacing postgresql with sqlite3 block from databases.example.yml
did the job, sqlite3:build_databases
executed successfully. This file should be mentioned in README_tests.txt![bundle exec] rake sqlite:test
ends up with LoadError, trying to load connection.rb
from non-existent directory test/connections/native_sqlite3/
Renaming test/connections/native_sqlite
directory to native_sqlite3
allows me to do testing, however other sqlite tasks depend on the former naming.
I think this is a result of confusion between sqlite
and sqlite3
tasks (all sqlite tasks use sqlite3
namespace, except sqlite:test
).
I think that the easiest solution would be to fix sqlite3:load_connection
in tasks/databases/sqlite3.rake#27 and renaming test/connections/native_sqlite
directory to native_sqlite3
. I am working on this right now.
Even if you accept somewhat confusing naming of Rake tasks, README_tests.txt
should be updated to reflect recent changes in environment configuration and to save contributors from guesswork.
Version of composite_primary_keys: 2.3.5 (2.3.5.1 causes error see Issue #12 and http://groups.google.com/group/compositekeys/browse_thread/thread/a7119aefab97d8c3)
Version of activerecord: 2.3.14
If the composite keys include a column name which is the (auto) sequence_name of a table inserting a new row to that table causes an error. (at least of the odbc adapter and MS Access or MS SQL)
The attached patch fixes this problem:
fd8d563fcee164c72855ee9882cbce5d8e7180d6
lib/composite_primary_keys/base.rb | 17 ++++++++++++++---
1 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/lib/composite_primary_keys/base.rb b/lib/composite_primary_keys/base.rb
index 1aa3a8a..5a0b64d 100644
--- a/lib/composite_primary_keys/base.rb
+++ b/lib/composite_primary_keys/base.rb
@@ -68,6 +68,14 @@ module CompositePrimaryKeys
to_composite_ids
end
+ def quoted_id_insert #:nodoc:
+ [self.class.primary_keys, ids].
+ transpose.
+ map {|attr_name,id|
+ quote_value(id, column_for_attribute(attr_name)) unless attr_name == self.class.sequence_name.to_sym
+ }.compact.to_composite_ids
+ end
+
# Sets the primary ID.
def id=(ids)
ids = ids.split(ID_SEP) if ids.is_a?(String)
@@ -102,16 +110,19 @@ module CompositePrimaryKeys
raise CompositeKeyError, "Composite keys do not generated ids from sequences, you must provide id values"
end
attributes_minus_pks = attributes_with_quotes(false)
- quoted_pk_columns = self.class.primary_key.map { |col| connection.quote_column_name(col) }
+ quoted_pk_columns = self.class.primary_key.map { |col|
+ connection.quote_column_name(col) unless col == self.class.sequence_name.to_sym
+ }.compact
cols = quoted_column_names(attributes_minus_pks) << quoted_pk_columns
- vals = attributes_minus_pks.values << quoted_id
+ vals = attributes_minus_pks.values << quoted_id_insert
connection.insert(
"INSERT INTO #{self.class.quoted_table_name} " +
"(#{cols.join(', ')}) " +
"VALUES (#{vals.join(', ')})",
"#{self.class.name} Create",
self.class.primary_key,
- self.id
+ self.id,
+ self.class.sequence_name
)
@new_record = false
return true
Ruby changed the Array.to_s method in 1.9. It used to just squash the array elements but now it outputs a more user-friendly representation. This breaks one of the unit tests. I'm hesitant to just change it to match 1.9's output because I'm not really sure what the intent of the test is.
Hello,
I got the following error with cpk 3.1.6 and includes.
When master and detail table have both composite
primary keys and use some thing like this
MyModel.includes(:my_asso).where(...).first
I got the following error:
NoMethodError: undefined method to_sym' for [..., ...]:Array
The Problem is within associations.rb line 131. It doesn't respect
associations with composite primary keys.
My work around:
Replace
when :belongs_to
[aliased_table[options[:primary_key] || reflection.klass.primary_key].eq(parent_table[options[:foreign_key] || reflection.primary_key_name])]
end
with
when :belongs_to
foreign_key = options[:primary_key] || reflection.klass.primary_key
composite_join_predicates(aliased_table, foreign_key,
parent_table, options[:foreign_key] || reflection.primary_key)
end
It work's to me, but I don't know if it is the right solution.
I think test_associations.rb should be extented with testcases
where both master and detail tables have composite keys.
Regards
Dieter
In https://github.com/drnic/composite_primary_keys/blob/master/lib/composite_primary_keys/base.rb
set_primary_key(keys.first)
should be changed to self.primary_key = keys.first
Also, since Rails is deprecating set_primary_key(some_key)
syntax, maybe we should also override the primary_keys=
class method instead so that the syntax will be consistent.
So it will be something like:
self.primary_keys = ['key_1', 'key_2']
This code snippet is from lib/reflections.rb
def derive_primary_key
if options[:foreign_key]
options[:foreign_key]
elsif belongs_to?
#CPK
#"#{name}_id"
class_name.foreign_key
elsif options[:as]
"#{options[:as]}_id"
else
active_record.name.foreign_key
end
end
I wonder why the original rails code "#{name}_id" is commented and replaced by class_name.foreign_key?
My association which is declared as:
belongs_to :author, :class_name => 'User'
won't work because instead of defaulting to author_id as the foreign key, it is now using user_id.
The rails docs specifically says that the default foreign key is the name of the association appended with _id. Why change the default to base on the class_name?
Note: I see that reflections.rb has been completely removed from the master branch but we might need a fix for this for the old branch
Hi,
I have table with a composite primary key consisting of two varchars.
I've noted that when I call .exists?( id ) with id which is an array
of integers, the resulting sql query expands correctly:
irb(main):019:0> id = [ 1, 2 ]
irb(main):020:0> MyModel.exists?( id )
(0.6ms) SELECT 1 FROM my_model
WHERE (my_model
.key1
= 1 AND my_model
.key2
= 2) LIMIT 1
=> false
However, when I pass an array of strings (or a comma separated list of values) it doesn't expand correctly:
irb(main):017:0> id = [ '1', '2' ]
irb(main):020:0> MyModel.exists?( id )
(0.3ms) SELECT 1 FROM my_model
WHERE (1) LIMIT 1
I want to use exists? instead of find() as the former does not raise exceptions on fail ...
In test_associations.rb this test fails:
assert_equal 3, Tariff.count(:include => :product_tariffs)
This is because lib/composite_primary_keys/calculations.rb is commented out with a note "TODO - This code doesn't work with ActiveRecord 3.0.3..." I did some poking around and the solution will be non-trivial. The bug at the moment is that AR expects a single column name so it just calls to_s on the array containing the composite primary keys, which generates bad SQL (different for ruby 1.8 and 1.9).
The code in calculations.rb looks like it's trying to create a subquery and use that to find the distinct values then wrapping that in a query that does the count. I think this is because postgresql errors if the primary key column types are different, e.g.,
=> SELECT count(DISTINCT tariffs.tariff_id, tariffs.start_date) FROM "tariffs" LEFT OUTER JOIN "product_tariffs" ON "product_tariffs"."tariff_id" = "tariffs"."tariff_id" AND "product_tariffs"."tariff_start_date" = "tariffs"."start_date";
ERROR: function count(integer, date) does not exist
LINE 1: SELECT count(DISTINCT tariffs.tariff_id, tariffs.start_date)...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The bug in the commented-out code is that it's trying to trick ARel by using the subquery as a table name but postgresql fails bacuase the subquery is longer than table names are allowed to be:
ArgumentError Exception: Input string is longer than NAMEDATALEN-1 (63)
Hi,
I have been trying to use :dependent => :destroy on some associations, and noticed that this does not work for any models that have more than one key. The destroy call on the parent model instance seems to go to the destroy method in composite_primary_keys/base.rb which does a delete_all. This prevents any before/after destroy call backs from getting run, and no dependent associations are destroyed.
Thanks,
Brandon Hauff
Using AR 2.3.5 and oracle_enhanced driver, 1.2.3. Here's a little bit of the stack trace:
element size differs (1 should be 2)
./lib/composite_primary_keys/lib/composite_primary_keys/associations.rb:297:in transpose' ./lib/composite_primary_keys/lib/composite_primary_keys/associations.rb:297:in
full_columns_equals'
./lib/composite_primary_keys/lib/composite_primary_keys/associations.rb:389:in construct_sql' /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_one_association.rb:6:in
initialize'
Here are my clases:
class Policy
set_primary_keys :policy_number, :policy_type
end
class Vendor
def library_type
"LIBR"
end
set_primary_key :vendor_key
has_one(
:library_policy,
:primary_key => [:library, :library_type],
:foreign_key => [:policy_number, :policy_type],
:class_name => 'Policy'
)
end
I'm getting this error when I try to run 'rake mysql:rebuild_databases'
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create table reference_codes (
reference_type_id int not null,
reference' at line 9: create table reference_types (
reference_type_id int not null auto_increment,
type_label varchar(50) default null,
abbreviation varchar(50) default null,
description varchar(50) default null,
primary key (reference_type_id)
);
...
I don't know if this is related to the problems I had in #100...?
Anyway, executing the same SQL from the mysql client seems to work just fine so it seems like it must be related more to ActiveRecord than to the SQL itself being invalid:
mysql composite_primary_keys_unittest < ./test/fixtures/db_definitions/mysql.sql
Any ideas?
I am using 3.1.9 gem.
It appears, that fix introduced in 3.1.8 release doesn't fix it in a best way. Before that, it simply failed - now in certain configuration (when owner doesn't use CPK) it can remove unrelated entries.
The cause is that, @owner.id can be [1,2,3,4]:Array
- when using CPK I think and what is expected by current code, but when don't using CPK its eg. 9:Fixnum
- and ruby can evaluate statement like this 9[0]
which gives 1
Problem is (as you can expect) in delete_record
function, simple (but not very elegant) fix looks like this:
if @reflection.cpk_primary_key.count == 1
owner_conditions << relation[@reflection.cpk_primary_key.first.to_sym].eq(@owner.id)
else
@reflection.cpk_primary_key.each_with_index do |column,i|
owner_conditions << relation[column.to_sym].eq(@owner.id[i])
end
end
These tests will fail:
def test_polymorphic_has_many
comments = User.find(1).comments
assert_equal 1, comments.length
assert_equal 'User', comments[0].person_type
assert_equal 1, comments[0].person_id
end
def test_polymorphic_has_one
first_comment = User.find(1).first_comment
assert_equal 'User', first_comment.person_type
assert_equal 1, first_comment.person_id
end
with:
7) Failure:
test_polymorphic_has_many(TestPolymorphic)
[/home/mike/code/oss/composite_primary_keys/test/test_polymorphic.rb:8:in `test_polymorphic_has_many'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:67:in `run'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:399:in `_run_setup_callbacks'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `send'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<1> expected but was
<2>.
8) Failure:
test_polymorphic_has_one(TestPolymorphic)
[/home/mike/code/oss/composite_primary_keys/test/test_polymorphic.rb:15:in `test_polymorphic_has_one'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:67:in `__send__'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:67:in `run'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:399:in `_run_setup_callbacks'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `send'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/mike/.bundler/ruby/1.8/gems/activesupport-3.1.0/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<"User"> expected but was
<"Employee">.
The key point being that the SQL query is not selecting on the polymorphic type -- only on the id. e.g.,
Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`person_id` = 1
instead of
Comment Load (0.4ms) SELECT `comments`.* FROM `comments` WHERE `comments`.`person_id` = 1 AND `comments`.`person_type` = 'User'
Rails 3.0.3
CPK 3.0.7
PostgreSQL 8.x
The following code that uses a unique + scope validation (to validate uniqueness for a two column combo) is broken with CPK. Instead of getting "has already been taken", I'm getting "translation missing: en, activerecord, errors, models, location, attributes, siteid, taken".
require 'composite_primary_keys'
require 'active_record'
class Location < ActiveRecord::Base
validates :siteid,
:length => {:is => 6},
:uniqueness => {:scope => :schoolid},
:presence => true
end
location = Location.new
location.schoolid = 'SOMEXX'
location.siteid = 'ATM-01'
p location.valid?
p location.errors
Using rails 3.2.0 and composite_primary_keys 5.0.0.rc1 (and previous versions), I have the following:
class Event < ActiveRecord::Base
self.pluralize_table_names = false
set_primary_keys [:sid, :cid]
belongs_to :iphdr, foreign_key: [:sid, :cid], :dependent => :destroy
class Iphdr < ActiveRecord::Base
self.pluralize_table_names = false
set_primary_keys [:sid, :cid]
has_many :events, foreign_key: [:sid, :cid]
These queries
@events = Event.includes(:iphdr).where("iphdr.ip_src = ?", 329777412).count
... or ...
@events = Event.includes(:iphdr).where("iphdr.ip_src = ?", 329777412).size
cause the following error:
Mysql2::Error: Column 'sid' in field list is ambiguous:
SELECT COUNT(*) FROM (SELECT DISTINCT sid,cid
FROM `event` LEFT OUTER JOIN `iphdr`
ON `iphdr`.`sid` = `event`.`sid`
AND `iphdr`.`cid` = `event`.`cid`
WHERE (iphdr.ip_src = 329777412)) subquery_for_count
As both tables have the same primary keys, I think the SELECT DISTINCT sid,cid
should be SELECT DISTINCT event.sid, event.cid. After forking CPK I changed the
following in build_count_subquery method in lib/composite_primary_keys/relation/calculations.rb:
... from:
relation.select_values = ["DISTINCT #{column.to_s}"]
... to this:
if column_name.is_a?(CompositePrimaryKeys::CompositeKeys)
relation.select_values = ["DISTINCT #{column.to_s.split(',').collect {|col| @klass.unscoped.table.name + '.' + col.strip}.join(', ')}"]
else
relation.select_values = ["DISTINCT #{column.to_s}"]
end
I am unsure if this is a proper fix, but it works for MySQL ... but maybe there is a better way to express the query.
Thanks for CPK.
I have 2 models Category and Keyword (a Category has many Keywords)
class Category < ActiveRecord::Base
set_primary_key :code
has_many :keywords, :foreign_key => :code
has_many :exclusions, :foreign_key => :code
has_many :category_attribute, :foreign_key => :code
def self.inheritance_column
nil
end
def self.autocomplete_term(hash)
Category.where("categories.text LIKE ?", "%#{hash[:term]}%").order("LENGTH(text)").limit(hash[:limit])
end
def as_json(options = {})
{
:code => self.code,
:text => self.text,
:type => self.type,
:mrank => self.mrank,
:exclusions => self.exclusions,
:text_stemmed => self.text_stemmed,
:keywords => self.keywords.map {|k| k}
}
end
end
class Keyword < ActiveRecord::Base
set_primary_keys :code, :keyword
belongs_to :category, :foreign_key => :code
end
The weird thing is that if I do
cat = Category.find(...)
cat.keywords.to_json() returns an empty json string even though cat.keywords returns the keywords that belong to that category.
Hi,
after we upgraded our application to rails 2.3.4 (and even 2.3.5), we had a problem to build a new database with the ActiveRecord::Migrations of our rails-application.
We are using ruby 1.8.7 and a MySQL 5.0 database.
the migration for a Message:
class CreateMessages < ActiveRecord::Migration
def self.up
create_table :messages, :primary_key => [:category, :subcategory, :k] do |t|
t.string :category, :limit => 80
t.string :subcategory, :limit => 80
t.string :k, :limit => 80
t.text :value
t.timestamps
end
end
and the class definition:
class Message < ActiveRecord::Base
set_primary_keys :category, :subcategory, :k
...
end
With AR 2.3.3 + CPK 2.3.2 the table was build as it should be:
CREATE TABLE messages
(
category
varchar(80) NOT NULL default '',
subcategory
varchar(80) NOT NULL default '',
k
varchar(80) NOT NULL default '',
value
text,
created_at
datetime default NULL,
updated_at
datetime default NULL,
PRIMARY KEY (category
,subcategory
,k
)
)
With AR 2.3.4 (or 2.3.5) and CPK 2.3.2 (or 2.3.5.1) something went wrong with the Primary Key:
CREATE TABLE `messages` (
`categorysubcategoryk` int(11) NOT NULL auto_increment,
`category` varchar(80) default NULL,
`subcategory` varchar(80) default NULL,
`k` varchar(80) default NULL,
`value` text,
`created_at` datetime default NULL,
`updated_at` datetime default NULL,
PRIMARY KEY (`categorysubcategoryk`)
)
We are not very familiar with the code of ActiveRecord or Composite Primary Keys. But finally we found a hint in the code of ActiveRecord::ConnectionAdapters::ColumnDefinition
: the line alias to_s :to_sql
was removed in AR 2.3.4.
So we modified the file lib/composite_primary_keys/migration.rb
to call to_sql
instead of to_s
:
diff --git a/lib/composite_primary_keys/migration.rb b/lib/composite_primary_keys/migration.rb
index 2a50404..2530bd7 100644
--- a/lib/composite_primary_keys/migration.rb
+++ b/lib/composite_primary_keys/migration.rb
@@ -1,7 +1,7 @@
-ActiveRecord::ConnectionAdapters::ColumnDefinition.send(:alias_method, :to_s_without_composite_keys, :to_s)
+ActiveRecord::ConnectionAdapters::ColumnDefinition.send(:alias_method, :to_s_without_composite_keys, :to_sql)
ActiveRecord::ConnectionAdapters::ColumnDefinition.class_eval <<-'EOF'
- def to_s
+ def to_sql
if name.is_a? Array
"PRIMARY KEY (#{name.join(',')})"
else
So far, this patch is working fine for us, but we are not really sure if this change has side effects.
Please, could someone have a look at that 'solution'? We would be glad to get some feedback...
regards
Ingo
After upgrading to ActiveREcord 3.0.3 and CPK 3.1.0, I kept getting uninitialized constant: CompositePrimaryKeys::ActiveRecord::Base. Upon further inspection it seemed that this was causing the problem:
def id_before_type_cast #:nodoc:
raise CompositeKeyError, CompositePrimaryKeys::ActiveRecord::Base::NOT_IMPLEMENTED_YET
end
FormHelper was unable to access the class containing the NOT_IMPLEMENTED_YET constant. Of course, even had this not failed, an exception would have been raised instead of allowing FormHelper to get what it needed, so my solution was:
def id_before_type_cast #:nodoc:
self.class.primary_keys.map {|key| self.send(key.to_s + "_before_type_cast")}
end
Not overly elegant, but works similarly to ActionView::Helpers::FormHelper::InstanceTagMethods#value_before_type_cast. It's a bit early in testing, but so far it's worked like a charm. I was wondering if anyone had any thoughts/comments/concerns about the implementation. My fork is at: https://github.com/canweriotnow/composite_primary_keys
As Rails 3 nears it would be nice to see what is necessary to get cpk working with it.
I am rewriting an old application into Rails 3.1, and I wonder if it's possible to generate such composite keys :
CREATE TABLE users
(
id
int(11) NOT NULL auto_increment,
account_id
int(11) default NULL,
display_name
varchar(255) NOT NULL default '',
profile_id
int(11) default NULL,
..
...
PRIMARY KEY (id
),
UNIQUE KEY by_account_token
(account_id
,token
),
UNIQUE KEY by_profile
(profile_id
),
KEY by_account_display
(account_id
,display_name
),
KEY by_account_profile
(account_id
,profile_id
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
If not I guess I have to generate the schema by hand and then upload it ...
Perhaps the issue is more with the choice of mysql, and I'm not entirely sure the issue is composite_primary_keys' fault. But here is my symptom.
In AR model Student
, add
self.primary_keys = :id, :district_id
Run my queries and see this slow one:
(1723.0ms) SELECT COUNT(*) FROM (SELECT DISTINCT `students`.* FROM `students` WHERE `students`.`district_id` = 3
AND ((home_campus_id in (1,2,3,4,5,6,7,8,9,10,11,12,13) or home_campus_id is null) and (choice_campus_id in
(1,2,3,4,5,6,7,8,9,10,11,12,13) or choice_campus_id is null) and grade_level <= '10' and grade_level >= '09'))
subquery_for_count
Remove the self.primary_keys = :id, :district_id
from Student model and see this query in it's place:
(8.0ms) SELECT COUNT(*) FROM `students` WHERE `students`.`district_id` = 3 AND ((home_campus_id in
(1,2,3,4,5,6,7,8,9,10,11,12,13) or home_campus_id is null) and (choice_campus_id in (1,2,3,4,5,6,7,8,9,10,11,12,13) or
choice_campus_id is null) and grade_level <= '10' and grade_level >= '09')
Same query except for the subquery.
So my question is:
Is there a reason to go with the subquery?
If not, could I remove it and submit a pull request? (note, I haven't yet looked through the code, I'm just wondering out loud about what's going on, and if removing the subquery is a valid solution you'd be willing to accept).
Thanks for your input!
Node's to_sql method assumes ActiveRecord::Base's engine if none is given, so in Predicates' cpk_or_predicate method the call need the engine specified. Otherwise when using sub-classes of Base/multiple databases, quoting and such is wrong. Something like predicate.to_sql(predicate.children.first.left.relation.engine)
Using CPK 3.1.10 on Rails 3.0.9.
You can reproduce this problem using this User model:
class User < ActiveRecord::Base
has_many :articles
belongs_to :favorite_article, :class_name => 'Article', :foreign_key => [:id, :favorite_article_id]
end
After adding the following line to an ERB file for users:
collection_select(:user, :favorite_article_id, @user.articles, :id, :article_id)
...the generated dropdown has the wrong option selected (it simply selects the first one). It looks like ActionView is comparing 'User.favorite_article_id' against 'Article.user_id,Article.article_id'.
A workaround is to pass ':selected => @user.favorite_article.id.to_s' to the arguments for collection_select, but it would be nice to have this handled automatically.
7a96012 added a statement to explicitly load active record's associations feature. This overwrites changes made by the autosave associations feature, namely: adding :autosave to the list of allowed keys for has_many etc.
Hi :)
composite_primary_keys (5.0.1)
activerecord (3.2.1) and activerecord (3.2.0)
class Form < ActiveRecord::Base
self.primary_keys = 'id_camp', 'id'
has_many :questions, :foreign_key => ['id_camp', 'id']
end
class Question < ActiveRecord::Base
self.primary_keys = 'id_camp', 'id_quest', 'id'
belongs_to :form, :foreign_key => ['id_camp', 'id_quest'];
end
I'm trying to save a new question object
form = Form.first
question = Question.new(:form => form)
question.save
Gives this error
["id_camp", "id_quest"] is not a symbol
While this code not:
form = Campaign.first.forms.first
question = form.questions.build
question.save
Using CPK 3.1.10 on Rails 3.0.9.
Using this User model:
class User < ActiveRecord::Base
has_many :articles
accepts_nested_attributes_for :articles
end
...and Article model and schema:
class Article < ActiveRecord::Base
set_primary_keys :user_id, :article_id
end
create_table "articles", :id => false, :force => true do |t|
t.integer "article_id"
t.integer "user_id"
t.string "content"
end
...and this ERB file:
<%= form_for @user do |f| %>
<%= f.fields_for :articles, @user.articles.first do |a| %>
<%= a.text_area :content %>
<% end %>
<%= submit_tag("Save") %>
<% end %>
...submitting the form causes the following exception to be thrown:
NoMethodError (undefined method `to_sym' for [:user_id, :article_id]:Array):
app/controllers/users_controller.rb:13:in `update'
activerecord (3.0.9) lib/active_record/base.rb:1267:in `block in expand_hash_conditions_for_aggregates'
activerecord (3.0.9) lib/active_record/base.rb:1266:in `each'
activerecord (3.0.9) lib/active_record/base.rb:1266:in `expand_hash_conditions_for_aggregates'
activerecord (3.0.9) lib/active_record/relation/query_methods.rb:225:in `build_where'
activerecord (3.0.9) lib/active_record/relation/query_methods.rb:76:in `where'
activerecord (3.0.9) lib/active_record/relation/spawn_methods.rb:124:in `apply_finder_options'
activerecord (3.0.9) lib/active_record/relation/finder_methods.rb:143:in `all'
activerecord (3.0.9) lib/active_record/base.rb:444:in `all'
activerecord (3.0.9) lib/active_record/associations/association_collection.rb:444:in `block in method_missing'
activerecord (3.0.9) lib/active_record/base.rb:1127:in `with_scope'
activerecord (3.0.9) lib/active_record/associations/association_proxy.rb:207:in `with_scope'
activerecord (3.0.9) lib/active_record/associations/association_collection.rb:440:in `method_missing'
activerecord (3.0.9) lib/active_record/nested_attributes.rb:364:in `assign_nested_attributes_for_collection_association'
activerecord (3.0.9) lib/active_record/nested_attributes.rb:254:in `articles_attributes='
activerecord (3.0.9) lib/active_record/base.rb:1564:in `block in attributes='
activerecord (3.0.9) lib/active_record/base.rb:1560:in `each'
activerecord (3.0.9) lib/active_record/base.rb:1560:in `attributes='
activerecord (3.0.9) lib/active_record/persistence.rb:131:in `block in update_attributes'
activerecord (3.0.9) lib/active_record/transactions.rb:292:in `block in with_transaction_returning_status'
activerecord (3.0.9) lib/active_record/connection_adapters/abstract/database_statements.rb:139:in `transaction'
activerecord (3.0.9) lib/active_record/transactions.rb:207:in `transaction'
activerecord (3.0.9) lib/active_record/transactions.rb:290:in `with_transaction_returning_status'
activerecord (3.0.9) lib/active_record/persistence.rb:130:in `update_attributes'
app/controllers/users_controller.rb:13:in `update'
users_controller.rb:13 is just a call to update_attributes.
Request parameters:
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"UxZZh4Mqrmy7A6oJdtWvgZKC/JAEFGI937HG2eEqcxE=",
"user"=>{"articles_attributes"=>{"0"=>{"content"=>"Writing an article...",
"id"=>"[1,
1]"}}},
"commit"=>"Save",
"id"=>"1"}
The specs were run against sqlite and postgresql.
3.1
Error:
test_composite_belongs_to_composite(TestAssociations):
NoMethodError: undefined method find_by_dorm_idroom_id' for Room(dorm_id: integer, room_id: integer):Class /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:989:in
method_missing'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/associations/belongs_to_association.rb:59:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/associations/belongs_to_association.rb:59:in
find_target'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:241:in load_target' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/associations/association_proxy.rb:118:in
reload'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/associations.rb:1442:in room' ./test/test_associations.rb:156:in
test_composite_belongs_to_composite'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
run'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in _run_setup_callbacks' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in
run'
Error:
test_find_includes_comp_belongs_to_tariff(TestAssociations):
NoMethodError: undefined method tariff_id,tariff_start_date' for #<ProductTariff:0x10245d5c8> /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in
method_missing'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:46:in method_missing' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:44:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:44:in method_missing' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in preload_one_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:116:in
preload_one_association'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:92:in preload_associations' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in to_a' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in to_a' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:143:in
all'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:101:in find' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in find' ./test/test_associations.rb:86:in
test_find_includes_comp_belongs_to_tariff'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
run'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in _run_setup_callbacks' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in
run'
Error:
test_find_includes_extended(TestAssociations):
NoMethodError: undefined method tariff_id,tariff_start_date' for #<ProductTariff:0x1024394c0> /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in
method_missing'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:46:in method_missing' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in preload_one_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:116:in
preload_one_association'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:92:in preload_associations' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:101:in
preload_associations'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:94:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:94:in
preload_associations'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
to_a'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
to_a'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:143:in all' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in find' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:101:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in
find'
./test/test_associations.rb:102:in test_find_includes_extended' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
Error:
test_has_many_through(TestAssociations):
NoMethodError: undefined method tariff_id,tariff_start_date' for #<ProductTariff:0x1023c2af0> /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activemodel-3.0.9/lib/active_model/attribute_methods.rb:392:in
method_missing'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/attribute_methods.rb:46:in method_missing' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:329:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:328:in preload_belongs_to_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in preload_one_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:116:in
preload_one_association'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:92:in preload_associations' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:254:in
preload_has_many_association'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:122:in
preload_one_association'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/ordered_hash.rb:139:in
each'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:116:in preload_one_association' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/association_preload.rb:92:in
preload_associations'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
to_a'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in each' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation.rb:68:in
to_a'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:143:in all' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:105:in find' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:101:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in
find'
./test/test_associations.rb:112:in test_has_many_through' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
Failure:
test_has_many_with_primary_key_with_associations(TestAssociations)
[./test/test_associations.rb:223:in test_has_many_with_primary_key_with_associations' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<1> expected but was
<0>.
Failure:
test_has_one_with_composite(TestAssociations)
[./test/test_associations.rb:167:in test_has_one_with_composite' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
expected to not be nil.
Failure:
test_limitable_reflections(TestAssociations)
[./test/test_associations.rb:232:in test_limitable_reflections' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<1> expected but was
<0>.
Error:
test_read_attribute_with_composite_keys(TestAttributeMethods):
ActiveRecord::RecordNotFound: Couldn't find ReferenceCode with ID=1,1 WHERE "reference_codes"."reference_type_id" = 1 AND "reference_codes"."reference_code" = 1
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/finder_methods.rb:112:in find_with_ids' /Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/finder_methods.rb:92:in
map'
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/finder_methods.rb:92:in find_with_ids' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/relation/finder_methods.rb:107:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.0.9/lib/active_record/base.rb:444:in
find'
./test/test_attribute_methods.rb:14:in test_read_attribute_with_composite_keys' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/callbacks.rb:419:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.0.9/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
101 tests, 261 assertions, 3 failures, 5 errors
master:
Failure:
test_has_many_with_primary_key_with_associations(TestAssociations)
[./test/test_associations.rb:225:in test_has_many_with_primary_key_with_associations' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<1> expected but was
<0>.
Failure:
test_has_one_with_composite(TestAssociations)
[./test/test_associations.rb:169:in test_has_one_with_composite' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
expected to not be nil.
Failure:
test_limitable_reflections(TestAssociations)
[./test/test_associations.rb:234:in test_limitable_reflections' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<1> expected but was
<0>.
Error:
test_read_attribute_with_composite_keys(TestAttributeMethods):
ActiveRecord::RecordNotFound: Couldn't find ReferenceCode with ID=1,1 WHERE "reference_codes"."reference_type_id" = 1 AND "reference_codes"."reference_code" = 1
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:107:in find_with_ids' /Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in
map'
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in find_with_ids' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:107:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in
find'
./test/test_attribute_methods.rb:14:in test_read_attribute_with_composite_keys' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
Error:
test_to_key_with_composite_key_destroyed(TestAttributeMethods):
ActiveRecord::RecordNotFound: Couldn't find ReferenceCode with ID=1,1 WHERE "reference_codes"."reference_type_id" = 1 AND "reference_codes"."reference_code" = 1
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:107:in find_with_ids' /Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in
map'
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in find_with_ids' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:107:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in
find'
./test/test_attribute_methods.rb:49:in test_to_key_with_composite_key_destroyed' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
Error:
test_to_key_with_composite_keys(TestAttributeMethods):
ActiveRecord::RecordNotFound: Couldn't find ReferenceCode with ID=1,1 WHERE "reference_codes"."reference_type_id" = 1 AND "reference_codes"."reference_code" = 1
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:107:in find_with_ids' /Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in
map'
/Users/milandobrota/Desktop/composite_primary_keys/lib/composite_primary_keys/relation/finder_methods.rb:87:in find_with_ids' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/relation/finder_methods.rb:107:in
find'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in __send__' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activerecord-3.1.0.rc5/lib/active_record/base.rb:437:in
find'
./test/test_attribute_methods.rb:27:in test_to_key_with_composite_keys' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run'
Failure:
test_to_key_with_single_key_destroyed(TestAttributeMethods)
[./test/test_attribute_methods.rb:45:in test_to_key_with_single_key_destroyed' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'
/Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:65:in `run']:
<[1]> expected but was
.
104 tests, 264 assertions, 4 failures, 3 errors
What is weird, the second time I run it I get:
test_to_key_with_single_key_destroyed' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/testing/setup_and_teardown.rb:67:in
send'run' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:399:in
_run_setup_callbacks'send' /Users/milandobrota/.rvm/gems/ree-1.8.7-2011.03@composite/gems/activesupport-3.1.0.rc5/lib/active_support/callbacks.rb:81:in
run_callbacks'104 tests, 272 assertions, 1 failures, 0 errors
The homepage link for composite_primary_keys on https://rubygems.org/gems/composite_primary_keys points to what appears to a dead fork from Nov. 2010:
http://github.com/cfis/composite_primary_keys
This made the project appear out of date when trying to find what was being fixed for Rails 3.2 compatibility.
I'm using Rails 3.1.0.rc5 with composite_primary_keys beta7. I have a model with a composite key (consisting of a string and an integer) called Post. The corresponding sqlite3 table has around 40,000 rows. When I execute Post.count
, I would normally expect the resulting query to be:
SELECT COUNT(*) FROM "newsgroups"
...which would usually take less than 1ms. This is in fact the case when I disable the gem and comment out the set_primary_keys
line in the model. However, with the gem enabled, this query is executed instead:
SELECT COUNT(*) FROM (SELECT DISTINCT * FROM "posts" ) subquery_for_count
...which takes upwards of four seconds. I have no idea what the "subquery_for_count" at the end is supposed to be (it seems to be simply ignored), but needless to say, this really slows things down. The problem also occurs with other ActiveRecord methods that use COUNT, such as .any?
.
In CPK 4.0.0.rc2, models with composite primary keys don't work with form_for
in Rails, due to problematic ActiveRecord::ActiveRecord::PrimaryKey::to_key
method (source). send
throws a TypeError
when self.class.primary_key
returns an array. As far as I know, this method is used to generate unique DOM IDs for forms - luckily, ActionPack can handle CPKs without any problems.
I've made a workaround to this problem by overriding this method in my model:
def to_key
if self.composite?
id unless id.compact.empty?
else
key = send(self.class.primary_key)
[key] if key
end
end
Here some description:
http://groups.google.com/group/compositekeys/browse_thread/thread/bebe45a20059a02
after installing the composite_primary_keys (even 2.3.5) there is warning :
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/
associations.rb:42: warning: already initialized constant
HasManyThroughCantAssociateThroughHasManyReflection
Cpk works fine while using find
with :include
option:
Tariff.find(:all, :include => :product_tariffs)
but has a problem with rails 3 includes
finder:
Tariff.includes(:product_tariffs)
which raises an SQL error like:
Unknown column 'tariff_idstart_date' in 'field list': SELECT COUNT(DISTINCT ...)
caused by line 71 at active_record/relation/calculations.rb
column_name = primary_key if column_name == :all
This can be bypassed with primary_key.join(", ")
instead of primary_key
here, but we should solve it more nicely.
I'm trying to add new rows to a join model ShopArticle with two primary keys article_id, shop_id.
class Shop < ActiveRecord::Base
has_many :shop_articles, :foreign_key => [:shop_id]
has_many :articles, :through=>:shop_articles, :foreign_key => [:shop_id]
...
class Article < ActiveRecord::Base
has_many :shop_articles, :foreign_key => [:article_id]
has_many :shops, :through=>:shop_articles, :foreign_key => [:article_id]
...
s=Shop.find 1
a=Article.find 113604
s.articles << a
This generates the following error:
ActiveRecord::StatementInvalid: PGError: ERROR: column "shop_id,article_id" does not exist
LINE 1: ... "article_id") VALUES (NULL, 1, 114389) RETURNING "shop_id,article_id"
^
: INSERT INTO "shop_articles" ("price", "shop_id", "article_id") VALUES (NULL, 1, 113604) RETURNING "shop_id,article_id"
So PostgreSQL wants RETURNING "shop_id","article_id" which it is not getting here.
I tracked this error down to /gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:490 and then noticed that CPK already has a modified connection adapter in lib/cpk/connection_adapters/postgresql_adapter.rb which should correctly quote the column names.
How is it possible that in my configuration, this connection adapter is not executed?
Log says:
: INSERT INTO "shop_articles" ("price", "shop_id", "article_id") VALUES (NULL, 1, 114389) RETURNING "shop_id,article_id"
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract_adapter.rb:219:in `log'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:550:in `execute'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:1037:in `select_raw'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:1024:in `select'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:13:in `select_one'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/abstract/database_statements.rb:19:in `select_value'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/connection_adapters/postgresql_adapter.rb:490:in `insert'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/composite_primary_keys-2.3.5.1/lib/composite_primary_keys/base.rb:108:in `create_without_callbacks'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:266:in `create'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2874:in `create_or_update_without_callbacks'
from /Users/leonhardtwille/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/callbacks.rb:250:in `create_or_update'
CPK 4.0.0.beta2, Rails 3.1.0.rc4
Once I've installed CPK, RoR started to complain about pg
gem missing in Gemfile even though the application doesn't use PostgreSQL:
% rails c
[...]/.rvm/gems/ruby-1.9.2-p180@rails/gems/bundler-1.0.15/lib/bundler/rubygems_integration.rb:143:in `block in replace_gem': pg is not part of the bundle. Add it to Gemfile. (Gem::LoadError)
PostgreSQL adapter is explicitly required in lib/composite_primary_keys.rb on line 54 and through its override on line 88.
I can remove these two lines or install pg
gem - both solutions seem to work fine with SQLite.
Could you please change this behavior, so the PostgreSQL adapter is loaded only if application uses it? Thanks!
I have a question regarding to composite primary keys gem. Previously, I use composite primary keys 2.3.2 with rails 2.3.2. Later, I upgrade composite primary keys to 2.3.5.1 since it fixes some bugs. However, after I install it, it also install some other gems as below:
actionmailer (2.3.2)
actionpack (2.3.2)
activemodel (3.0.4)
activerecord (3.0.4, 2.3.2)
activeresource (2.3.2)
activesupport (3.0.4, 2.3.2)
I wonder will there are any problems with this since composite primary keys depends on activesupport > 2.3.2 while rails 2.3.2 use activesupport 2.3.2?
Hi,
I am starting to use composite_primary_keys
.
On http://compositekeys.rubyforge.org/ I found this line:
Membership.find(:first).to_param # => "1,1"
But, I get "1-1" instead on Ruby on Rails 3.2.2.
Actually, ActiveModel::Conversion#to_param
is defined as follows:
def to_param persisted? ? to_key.join('-') : nil end
So, when I write an ERB code <%= link_to "Link", membership %>
, I get a link to /memberships/1-1
, which can't be handled correctly with Membership.find(params[:id])
.
Could you give me an advice?
Lots of forks, but no one steering the ship. I see 4 separate forks that have been published, all in various states. We desperately need a definitive release that addresses the superclass mismatch and works with Rails 3. Anyone got something out there ready to take the lead on this?
Looks like CPK associations have gotten slightly wonky:
current_user.address_most_recent_home_uk
home_tag = Tag.named(:home).id uk = Country.find_by_country("United Kingdom") person_address = person_addresses.first(:include => :address, :conditions => ["tag_id = ? and addresses.country_id = ?", home_tag, uk], :order => "addresses.updated_on desc") person_address.address if person_address
with person:
has_many :person_addresses, :foreign_key => "person_id", :dependent => :destroy has_many :addresses, :through => :person_addresses, :order => 'addresses.updated_on desc'
and person_addresses:
set_primary_keys :address_id, :person_id belongs_to :address belongs_to :person
results in:
ActiveRecord::StatementInvalid: PGError: ERROR: syntax error at or near "," LINE 1: ...untry_id = 1)) AND "person_addresses".address_id,person_id... ^ : SELECT "person_addresses"."address_id" AS t0_r0, "person_addresses"."person_id" AS t0_r1, "person_addresses"."tag_id" AS t0_r2, "person_addresses"."updated_on" AS t0_r3, "person_addresses"."updated_by" AS t0_r4, "person_addresses"."created_on" AS t0_r5, "person_addresses"."created_by" AS t0_r6, "addresses"."id" AS t1_r0, "addresses"."address_to" AS t1_r1, "addresses"."address_1" AS t1_r2, "addresses"."address_2" AS t1_r3, "addresses"."address_3" AS t1_r4, "addresses"."city" AS t1_r5, "addresses"."state_prov_reg" AS t1_r6, "addresses"."postal_code" AS t1_r7, "addresses"."country_id" AS t1_r8, "addresses"."updated_on" AS t1_r9, "addresses"."updated_by" AS t1_r10, "addresses"."created_on" AS t1_r11, "addresses"."created_by" AS t1_r12 FROM "person_addresses" LEFT OUTER JOIN "addresses" ON ("addresses"."id"="person_addresses"."address_id") WHERE (("person_addresses"."person_id" = 228253) AND (tag_id = 1 and addresses.country_id = 1)) AND "person_addresses".address_id,person_id IN (NULL) ORDER BY addresses.updated_on DESC from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/abstract_adapter.rb:221:in `log' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/postgresql_adapter.rb:520:in `execute' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/postgresql_adapter.rb:1002:in `select_raw' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/postgresql_adapter.rb:989:in `select' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/abstract/database_statements.rb:7:in `select_all_without_query_cache' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/connection_adapters/abstract/query_cache.rb:62:in `select_all' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/associations.rb:1622:in `select_all_rows' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/associations.rb:1401:in `find_with_associations' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/associations.rb:1399:in `catch' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/associations.rb:1399:in `find_with_associations' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/base.rb:1576:in `find_every' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/base.rb:1535:in `find_initial' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/base.rb:616:in `find' from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.7/lib/active_record/associations/association_collection.rb:60:in `find' from /Users/oqureshi/Sites/scholastic/ts-dev/store/app/models/person_module.rb:509:in `address_most_recent_home_uk'
4.0.0.beta7 depends on activerecord 3.1.0.rc5. I think it would be better to have ~> 3.1.0.rc5 to ease testing against later release candidates, such as the rc6 that just came out.
CPK 3.0.9 successfully queries models when the composite key is a string, such as '1,1'. CPK 3.1.0 breaks this, and does not seem to process the string at all, which leads to an exception when trying Model.find('1,1'):
RuntimeError (["1,1"]: Incorrect number of primary keys for Model: [:key1, :key2])
This appears to have started to go wrong around the time of merging a branch from wouter. Perhaps some of his branch got missed? Or maybe it got deleted later. There used to be a bit of code for splitting the string in the find_by_ids method.
This obviously breaks many things, such as Model.find(params[:id]) in any action of a controller.
I do this:
rails new testapp
cd testapp
rails g model datetimed_item open_at:datetime
rake db:migrate
Gemfile:
gem 'composite_primary_keys'
config/application.rb:
config.time_zone = 'Eastern Time (US & Canada)'
In the rails console I get this:
i=DatetimedItem.new( :open_at => '2010-09-15 12:00 pm' )
i.read_attribute( :open_at ).class
#Time
i.read_attribute(:open_at ).zone
#'UTC'
Without CPK, I get:
i.read_attribute( :open_at ).class
#ActiveSupport::TimeWithZone
i.read_attribute(:open_at ).zone
#'EDT'
This bug munges date entry through forms using datepicker/text fields... I think it has to do with attribute_writer but haven't been able to figure more at this point.
If one of the keys of the record you want, is nil (Legacy database, don't ask...), you can't just pass the ids as arguments.
Product.find(nil,9)
Results in
NoMethodError: undefined method `split' for 9:Fixnum
However, passing the ids in an array like this:
Product.find([nil,9])
Works.
Seeing as the workaround isn't too ugly, this could just be added to the documentation.
Hello,
I'm using cpk with Oracle and jruby / activerecord-jdbc-adapter.
ActiveRecord-JDBC-Adapter overrides arel_attributes_values (quoted_primary_key.rb),
but this change isn't working with cpk.
Is this a cpk or activerecord-jdbc-adapter issue?
Regards
Dieter
ActiveRecord::Associations::AssociationProxy#interpolate_sql has been removed in the latest Rails release (3.0.5). The new call is now ActiveRecord::Associations::AssociationProxy#interpolate_and_sanitize_sql which takes a sanitzing_klass as an arg. I'll work on getting a fix and waiting for your pull.
I have found out why CPK needs to load the active_record/associations.rb file to work. Its appears to be due to the fact that ActiveRecord has all its modules set to autoload. When CPK attempts to extended ActiveRecord::Associations it raises an uninitialized constant error. I am assuming that this is because the autoload is never triggered since a ActiveRecord::Association is never executed, but I admit I am grasping a bit as autoload is new to me. I only bring this up because we now get an already initialized constant error whenever we load CPK.
Any have this working or know of a workaround?
I know Rails 3.2 is still in RC2. But, when it's released, I think others will definitely run into this problem. I reproduced this error in an existing app I was trying to upgrade from Rails 3.1 as well as a new test app. Here is a simple user model:
# app/models/user.rb
class User < ActiveRecord::Base
end
# db/migrate/create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end
rake db:migrate
rails r "User.create :name => 'user1'"
rails r "puts User.first.inspect'" # throws stack level too deep (SystemStackError)
It generates the following warning message in development log:
DEPRECATION WARNING: You have called '_id'. This is deprecated. Please use either 'id' or read_attribute('id'). (called from <top (required)> at (eval):1)
DEPRECATION WARNING: The method `_id', matching the attribute `id' has dispatched through method_missing. This shouldn't happen, because `id' is a column of the table. If this error has happened through normal usage of Active Record (rather than through your own code or external libraries), please report it as a bug. (called from <top (required)> at (eval):1)
DEPRECATION WARNING: You have called '_id'. This is deprecated. Please use either 'id' or read_attribute('id'). (called from <top (required)> at (eval):1)
From a quick investigation, the problem seems to be in composite_primary_keys' ActiveRecord::AttributeMethods::Read#read_attribute.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.