List Info

Thread: ActiveRecord save problem




ActiveRecord save problem
country flaguser name
Germany
2008-01-21 03:40:09
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_i
d"])
# set new data
user_course.data= "test"
# save
user_course.save


thx
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
United Kingdom
2008-01-21 04:20:03

On 21 Jan 2008, at 09:40, Christian Kerth wrote:

>
> 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_i
d"])
> # 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


> thx
> -- 
> Posted via http://www.ruby-forum.com
/.
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 04:20:02
Is their an error appearing in the log?
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 04:30:43
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:
> On 21 Jan 2008, at 09:40, Christian Kerth wrote:
> 
>> update the data property.
>> 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

-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 04:40:22
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

======================================================
by convention your join tables should be the pluralized
forms of the two 
tables in alphabetical order (ie. courses_users) with the
model 
'CoursesUsers'

To start a few questions.

What are the field in the three tables?
If you find users_courses strait from the db (not through
the 
association) does it save successfully?

-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 05:04:34

> 
> To start a few questions.
> 
> What are the field in the three tables?

Fields are:

users
-----

  id         integer PRIMARY KEY AUTOINCREMENT NOT NULL,
  username   varchar(20),
  pass       varchar(20),
  pin        varchar(20),
  firstname  varchar(50),
  lastname   varchar(50),
  email      varchar(50)

courses
-------

  id                  integer PRIMARY KEY AUTOINCREMENT NOT
NULL,
  coursecategorie_id  integer,
  coursetype_id       integer NOT NULL,
  url                 varchar(255),
  fspath              varchar(255),
  lastupdate          datetime

users_courses
-------------

  user_id         integer NOT NULL,
  course_id       integer NOT NULL,
  coursestatus    integer,
  suspenddata     varchar(50),
  score           integer,
  startdate       datetime,
  expirationdate  datetime,
  language        varchar(20)


CREATE INDEX users_courses_FKIndex1
  ON users_courses
  (user_id);

CREATE INDEX users_courses_FKIndex2
  ON users_courses
  (course_id);

> If you find users_courses strait from the db (not
through the 
> association) does it save successfully?

No!

works:
------
user = User.find_by_id(req.query["user_id"])
user.pin = "haha"
user.save!


doesn't work:
-------------
only returns one row, because of the limited data in the
table
in the debugger everthing sheems ok.

uc =
UserCourse.find_by_course_id(req.query["course_id"
])
uc.suspenddata = "haha"
uc.save!


thx ck
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 05:47:36
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.
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 08:18:24
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.
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
United Kingdom
2008-01-21 08:44:04

On 21 Jan 2008, at 14:18, Christian Kerth wrote:

>
> 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

>
> -- 
> Posted via http://www.ruby-forum.com
/.
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: ActiveRecord save problem
country flaguser name
Germany
2008-01-21 08:54:43
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
-- 
Posted via http://www.ruby-forum.com
/.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talkgooglegroups.com
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---


[1-10]

about | contact  Other archives ( Real Estate discussion Medical topics )