Railsers:
Please blame SQLite3, not my feeb capacity to read the Agile book, and
all the blogs.
I want to do many-to-many between two tables. Classic relational stuff,
right?
So my schema looks a little bit like this:
create_table “users_props”, :force => true do |t|
t.column “prop_id”, :integer
t.column “user_id”, :integer
end
That links out to users and props tables, right? So their models
contain the sacred habtm incantations:
class User < ActiveRecord::Base
has_and_belongs_to_many :props
…
class Prop < ActiveRecord::Base
has_and_belongs_to_many :users
…
All too obvious to post, right? And all straight out of the Daves’
Agile book, right?
Now let’s put them in motion:
user.props << prop
That produces a huge error message like this:
ActiveRecord::StatementInvalid: ActiveRecord::StatementInvalid
/usr/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/sqlite_adapter.rb:259:in
table_structure' /usr/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/core_ext/object/misc.rb:23:in
returning’
I know how to fix that. I replace the syntactic sugar with this
old-skool SQL:
Prop.find_by_sql("""
insert into users_props(user_id, prop_id)
values(#{user.id}, #{prop.id})
""")
Okay. The record is probably there (I honestly didn’t check.) Now I
try to read it out:
assert_equal 1, quentin.props.count
Now check what that gives:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table:
props_users: SELECT count(*) AS count_all FROM props INNER JOIN
props_users ON props.id = props_users.prop_id WHERE
(props_users.user_id = 1 )
Why is it asking for props_users when I have a users_props?
–
Phlip
http://c2.com/cgi/wiki?ZeekLand ← NOT a blog!!