associations

Why am I getting org.hibernate.id.IdentifierGenerationException?

坚强是说给别人听的谎言 提交于 2019-12-08 11:45:08
问题 As I run my main class (Runner) program I get the following exception : org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: country I don't know the reason, why am I getting this exception. The mapping xml : <hibernate-mapping> <class name="pojo.Country" table="country"> <id name="countryID" column="c_id"> <generator class="increment" /> </id> <property name="countryName" column="c_name" /> <one-to-one class="pojo.PM" name="pm" cascade="all" /

Rails associations - Users has many Users through Comments

感情迁移 提交于 2019-12-08 11:35:34
问题 I'm trying to design a comment system allowing users to post on other users' pages, through comments. A user will have a comment on his page, which is posted by another user called "commenter." 1) Is the following code legit/functional/decent design? 2) Is it OK to have a renamed "commenter" user, in conjunction with an un-renamed "user", or should all association names of user always be renamed semantically? 3) Are there better ways to implement this design intent (e.g. not doing a has_many

Polymorphic Associations in Rails

自作多情 提交于 2019-12-08 10:15:39
问题 How do polymorphic associations work in Rails? What are their advantages? Is there a way to add belongs_to method just by running a migration? 回答1: Ryan has a railscast about this that is pretty good. Belongs_to isn't something you add to a migration, you add it to the model. In the migration, you have to add the foreign key column. For example if you have a post model that belongs to a user, you'd add the user_id column to the post activerecord in a migration. Then you add belongs_to :user

Rails - one model with two associations

一曲冷凌霜 提交于 2019-12-08 07:57:06
问题 I have the model Member that contains all informations about the registered member on my site. Then I have the model Message which contains 2 columns (actually 3 with id ): - member_id - message_from In the table Messages are stored IDs of user, how chatting together - when the member A send message to member B , so in the column member_id is saved ID of person A and into the column message_from ID of the person B . My current associations looks like: class Member < ActiveRecord::Base has

Deleting Records From Associated Table With PatchEntity in CakePHP

感情迁移 提交于 2019-12-08 07:02:31
问题 I have a CakePHP model where a Job hasmany Employees and an Employee belongsTo a Job. The View I use to edit both Jobs and Employees is a Job view. Here is code from my Job Controller: $job = $this->Jobs->get($id, [ 'contain' => ['Employees'] ]); if ($this->request->is(['patch', 'post', 'put'])) { $req = $this->request->data; $job = $this->Jobs->patchEntity($job, $req, [ 'validate' => false, 'associated' => ['Employees'] ]); $saveResult = $this->Jobs->save($job, [ 'validate' => false,

Make sure has_many :through association is unique on creation

允我心安 提交于 2019-12-08 06:26:01
问题 If you are saving a has_many :through association at record creation time, how can you make sure the association has unique objects. Unique is defined by a custom set of attributes. Considering: class User < ActiveRecord::Base has_many :user_roles has_many :roles, through: :user_roles before_validation :ensure_unique_roles private def ensure_unique_roles # I thought the following would work: self.roles = self.roles.to_a.uniq{|r| "#{r.project_id}-#{r.role_id}" } # but the above results in

Rails Multiple Input Field in Form to One Integer Attribute in Model

不羁的心 提交于 2019-12-08 06:07:06
问题 I am trying to allow a user to input two different things in two different drop down menus from the same form and it will store an integer into a review table. I want the user to be able to select model_name in one drop down and manufacturer in another drop down. The result will store a bat_id integer into the form. (Telling you which bat the user is selecting) I have seen a couple questions about date & time but they store the values directly in the model. I am trying to store an integer -

cakePHP model associations (retrieving data deeper)

我怕爱的太早我们不能终老 提交于 2019-12-08 05:00:43
问题 I tried to find this everywhere but I cant. Here is my problem: Lets say I have models associated like this: Student (has many Memberships) Membership (belongs to Student and TeacherCourse) TeacherCourse (belongs to Teacher and Course) Teacher (has many TeacherCourses) Course (has many TeacherCourses) When I use membershipsController to send all membership to the view, I get only for example $membership['Membership']['id'] or $membership['TeacherCourse']['id'] BUT NOT $membership[

Ruby on Rails - Settting up Reviews functionality

折月煮酒 提交于 2019-12-08 04:57:23
问题 I am trying to set up a feature on my Ruby on Rails app that lets users to review pictures. I've followed this guide as a reference. http://ruby.about.com/od/rubyonrails/ss/blogpart4_4.htm From my experiences working on other Ruby on Rails projects, I think a Posts/Comments relationship model can be used here for a Pictures/Reviews relationship. I first generated a scaffold. rails g scaffold review name:string body:text picture:references I would like each picture page to have a separate page

Has_Many Through Associations and Nested Attributes

吃可爱长大的小学妹 提交于 2019-12-08 03:31:28
I'm trying to create a view allowing me to create and edit values of my joining table directly. This model is called 'hires'. I need to be able to create multiple rows in my joining table for when a child hires up to 2 books. I'm having some trouble and I suspect it's down to my associations... I have 3 models. Each Child can have 2 books: class Child < ActiveRecord::Base has_many :hires has_many :books, through: :hires end class Hire < ActiveRecord::Base belongs_to :book belongs_to :child accepts_nested_attributes_for :book accepts_nested_attributes_for :child end class Book < ActiveRecord: