has-many

Yii: HAS_MANY search

梦想的初衷 提交于 2019-12-23 22:07:35
问题 I have the following tables: user (id, cv_personal_data_id), cv_personal_data (id, firstname, surname, gender, address, ...), cv_laboral_exp (id, user_id, position, seniority,... ), cv_study (id, user_id, name, institution, average, ...), cv_language (id, user_id, language_name, writing_level, ...) In my User model I have defined the following relations: public function relations() { return array( 'cvLaboralExps' => array(self::HAS_MANY, 'CvLaboralExp', 'user_id'), 'cvLanguages' => array(self

Update a HasMany records

醉酒当歌 提交于 2019-12-23 05:07:52
问题 I am converting a working app from cakephp2 to cakephp3. I'm struggling to get a form that updates hasMany records to work. The app has the following structure: MODELS: use Cake\ORM\Table; use Cake\Validation\Validator; class AwardsTable extends Table { public function initialize(array $config) { $this->hasMany('Levels', ['sort' => 'sort_order']); } } namespace App\Model\Entity; use Cake\Auth\DefaultPasswordHasher; use Cake\ORM\Entity; class Award extends Entity { protected $_accessible = [

Rails 4 relationship issues, On login

為{幸葍}努か 提交于 2019-12-23 04:56:25
问题 I'm not sure what has changed getting this error when trying to login: ERROR: relation "tags" does not exist LINE 1: SELECT DISTINCT "tags".* FROM "tags" INNER JOIN "taggings" O... The tag model: class Tag < ActiveRecord::Base attr_accessor :unread_count, :user_feeds has_many :taggings has_many :feeds, through: :taggings end The tagging model: class Tagging < ActiveRecord::Base belongs_to :tag belongs_to :feed belongs_to :user end And the user relationships: class User < ActiveRecord::Base

Multiple has_many relationships to the same class

感情迁移 提交于 2019-12-23 04:24:29
问题 I'm building a football game and I'm having trouble creating Club and Match classes. I want to be able to do this: match = Match.find(2) match.home_club = <some club here> match.away_club = <other club here> And also this: club = Club.find(2) club.matches # Returns all matches where club plays home or away This is what I have now: class Club < ActiveRecord::Base has_many :matches end class Match < ActiveRecord::Base belongs_to :home_club, :class_name => "Club" belongs_to :away_club, :class

Is it possible to have a compound foreign key in rails?

▼魔方 西西 提交于 2019-12-22 05:58:22
问题 Suppose the following data schema: Usage ====== client_id resource type amount Billing ====== client_id usage_resource usage_type rate In this example, suppose I have multiple resources, each of which can be used in many ways. For example, one resource is a widget . Widgets can be foo ed and they can be bar ed. Gizmo s can also be foo ed and bar ed. These usage types are billed at different rates, possibly even different rates for different clients. Each occurence of a usage (of a resource)

Create association between two instancied objects

江枫思渺然 提交于 2019-12-22 04:52:23
问题 I have two models: (Albums and Product) 1) Inside Models Inside album.rb: class Album < ActiveRecord::Base attr_accessible :name has_many :products end Inside product.rb: class Product < ActiveRecord::Base attr_accessible :img, :name, :price, :quantity belongs_to :album end 2) Using " rails console ", how can I set the associations (so I can use "<%= Product.first.album.name %>")? e.g. a = Album.create( :name => "My Album" ) p = Product.create( :name => "Shampoo X" ) # what's next? how can i

Backbone-relational hasmany best practices

馋奶兔 提交于 2019-12-22 03:58:14
问题 I am new to Backbone-relational, I am not sure what is the right way to use HasMany. I have a Parent model which have many children (by "many" I mean thousands of children). In order to avoid performance issue, I query children by their foreign key: /child/?parent=1 , instead of create a huge list of child_ids in Parent . But seems this is not the way Backbone-relational work. So I am wondering what is the right way to handle this. 1, Change my json api to include list of child id in parent,

How to use constant in the ON condition in Yii2 hasMany relation

隐身守侯 提交于 2019-12-22 01:13:27
问题 I try to create a polymorphic association, what is common in Rails but unfortunately not in Yii2. As part of the implementation I need to define the relation: public function getImages() { return $this->hasMany(RecipeImage::className(), ['imageable_id' => 'id', 'imageable_type' => 'Person']); } But this doesn't work, because 'Person' is treated as an attribute of the current model, but it is a constant (class name for the polymorphic association). If I try to use 'andWhere' it adds the

Rails has_many through for has_many with multiple models

核能气质少年 提交于 2019-12-21 05:51:22
问题 What would be the best way to model the following situation: Word belongs_to :wordable, :polymorphic => true Phrase has_many :words, :as => :workable belongs_to :story Line has_many :words, :as => :wordable belongs_to :story Story has_many :lines has_many :phrases has_many :words, :through => :phrases has_many :words, :through => :lines I want to be able to do @story.words to get list of all words that are linked to a story either via lines or via phrases... Is that possible? 回答1: Try this:

Delete a Has-Many Relationship ONLY

元气小坏坏 提交于 2019-12-20 18:26:50
问题 I have a: has_and_belongs_to_many :friends, :join_table => "friends_peoples". To add a friend I do: @people.followers << @friend which create the relationship and a new person profile. Now I'd like to delete the relationship ONLY and not the person profile. I tried @people.friends.delete(guilty.id) but it deletes the person profile and not the relationship. Any idea? 回答1: Have you tried this? @people.friends.delete(guilty) 来源: https://stackoverflow.com/questions/3852017/delete-a-has-many