ActiveRecord save problem

Hey I have a problem with saving data to my sqlite db using active
record.

when i query an object from the db, update a property and save it using
the active record save command it returns true, but nothing happens in
the database.
the user_course object is the right object from the db, it just doesn't
update the data property.

# get user
user = User.find_by_id(req.query["user_id"]);
#get user-course data
user_course =
user.user_courses.find_by_course_id(req.query["course_id"])
# set new data
user_course.data= "test"
# save
user_course.save

thx

Is their an error appearing in the log?

Hey I have a problem with saving data to my sqlite db using active
record.

when i query an object from the db, update a property and save it
using
the active record save command it returns true, but nothing happens in
the database.
the user_course object is the right object from the db, it just
doesn't
update the data property.

# get user
user = User.find_by_id(req.query["user_id"]);
#get user-course data
user_course =
user.user_courses.find_by_course_id(req.query["course_id"])
# set new data
user_course.data= "test"
# save
user_course.save

call save! instead of save. This should throw an exception indicating
why it couldn't save which should point you at the direction of the
problem

Fred

save! also returns true

I think this has something to do with the m:n relation user ->
user_courses -> course

when i try to save a attribute of a user object ist works, when i try to
save to user_courses ist fails in the described way.

my models look like:

class Course < ActiveRecord::Base

  # set table name: courses
  set_table_name 'courses'

  # set primary key: id
  set_primary_key 'id'

  # users_courses (1:n)
  has_many :user_courses

  # users (m:n -> through table users_courses)
  has_many :users, :through => :user_courses

end

class User < ActiveRecord::Base

  set_table_name 'users'
  set_primary_key 'id'

  # users_courses (1:n)
  has_many :user_courses

  # courses (m:n -> through users_courses)
  has_many :courses, :through => :user_courses

end

class UserCourse < ActiveRecord::Base

  # set table name: users_courses
  set_table_name 'users_courses'

  # courses (1:n)
  belongs_to :course, :foreign_key => 'course_id'

  # users (1:n)
  belongs_to :user, :foreign_key => 'user_id'

end

can somebody help?

Frederick Cheung wrote:

to start Rails is convention over configuration. change your models to

class Course < ActiveRecord::Base

  # users_courses (1:n)
  has_many :user_courses

  # users (m:n -> through table users_courses)
  has_many :users, :through => :user_courses

end

class User < ActiveRecord::Base

  # users_courses (1:n)
  has_many :user_courses

  # courses (m:n -> through users_courses)
  has_many :courses, :through => :user_courses

end

class UserCourse < ActiveRecord::Base

  # set table name: users_courses
  set_table_name 'users_courses'

  # courses (1:n)
  belongs_to :course, :foreign_key => 'course_id'

  # users (1:n)
  belongs_to :user, :foreign_key => 'user_id'

end

To start a few questions.

What are the field in the three tables?

Fields are:

users

I'm going to go out on a limb and say that you don't have a problem
creating a new users_courses record.

I would put money that you need only add a primary key to the
users_courses table.

Keynan Pratt wrote:

I'm going to go out on a limb and say that you don't have a problem
creating a new users_courses record.

I would put money that you need only add a primary key to the
users_courses table.

i dont want to have a single primary key, i want a composite one that
consists out of user_id and course_id, like it's done in traditional db
engieering.

Keynan Pratt wrote:

I'm going to go out on a limb and say that you don't have a problem
creating a new users_courses record.

I would put money that you need only add a primary key to the
users_courses table.

i dont want to have a single primary key, i want a composite one that
consists out of user_id and course_id, like it's done in traditional
db
engieering.

ActiveRecord doesn't do composite prinmary keys.
Just in case, you know that
  # set table name: courses
  set_table_name 'courses'

  # set primary key: id
  set_primary_key 'id'
aren't actually needed.

Fred

Keynan Pratt wrote:

I would put money that you need only add a primary key to the
users_courses table.

Oh, yeah.... you were so right!

But i did achieve what i wanted (a composite primary key) with the
composite_primary_keys extension to AR. Sheems to work fine.

Thanks for pointing me in the right direction!

ck