relation

Query records through its belongs_to relation in Rails

谁都会走 提交于 2020-01-28 04:07:06
问题 I have an Activities model, and they belong_to a Location How do i select all the activities whose location.country = Australia? (for example) Can I do this within a scope? 回答1: The kind of query you're talking about is a join. You can try queries like this in the console like: Activity.joins(:locations).where('locations.country = "Australia"') This means that SQL is going to take all the activities and locations associated with then, find the locations where country=Australia, and then

Doctrine ORM Conditional Association

痴心易碎 提交于 2020-01-24 10:24:06
问题 i'm building a Q&A site and my questions, answers and comments are on the same posts table. But their postType is different. I can get answers for a question and comments for an answer with this association: /** * @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent") */ private $answers; /** * @OneToMany(targetEntity="Cms\Entity\Post", mappedBy="parent") */ private $comments; But i think this is not the correct way to do this because if i fetch a question both answers and comments are

Counting Models related through manyToMany only if a pivot attribute null - Laravel

那年仲夏 提交于 2020-01-17 05:13:12
问题 I am elaborating code acquired here ManyToMany relation - how update attribute in pivot table what I want: I want to get a collection of Activities related to any weekly Routine . The pivot table's atribute done_at tells me when any activity (task) was completed. I can list and later ->count() activities related to parent model in manyToMany relation: public function activities() { return $this->belongsToMany('App\Models\Activity', 'activity_routine', 'routine_id', 'activity_id')->withPivot(

Combine two ActiveRecord::Relation with OR, not AND, returning a Relation and not an Array to be able to paginate later

家住魔仙堡 提交于 2020-01-14 12:46:51
问题 a and b are ActiveRecord::Relation objects which return the same type of objects(Micropost objects in this case) a.class => ActiveRecord::Relation b.class => ActiveRecord::Relation a.first => Micropost(...) b.first => Micropost(...) #They both return the same type of objects c=a+b c.class => Array #This is not what i'm looking for c=a|b c.class => Array #Not what i'm looking for either c=(a or b) c.class => ActiveRecord::Relation #But it is just a, so it's wrong c==a => true a.merge(b) => []

Normalisation into BCNF

大憨熊 提交于 2020-01-13 19:42:07
问题 I have a relation looks like: R = (X,Y,C,D) and functional dependencies: FD = {XY -> CD, YC -> D, D -> X} I found out that the candidate keys are XY, YC, YD. In this case, I have 3 candidate keys... so how do I use them when I decompose the relation into BCNF? I've never done decomposition of the relation with more than one key... Thanks. 回答1: Relation 1: (Y,C,D) Relation 2: (D,X) 回答2: @KeyM 1.Find out the FD that violates BCNF In this case its D->X 2.Decompose into two relations R1,R2 R1(D,X

LoopbackJS / AngularJS find with relation hasOne include separated attribute

三世轮回 提交于 2020-01-06 19:38:12
问题 i'm making a find query in angularjs using loopbackapi , in mysql db. I have a model with a hasOne relation with another one. Empresa hasOne Operadora When i do a find in Empresa using include args, it brings me Operadora attributes, but it include the attributes inside Empresa, but i would like to have it separated. I tested using those examples: Empresa.findOne({ include: {"relation" : 'Operadora', 'as': 'Operadora'} Empresa.findOne({ include: {model: 'Operadora', 'as': 'Operadora'} Empresa

MySQL find all parents recursively [duplicate]

て烟熏妆下的殇ゞ 提交于 2020-01-06 04:23:25
问题 This question already has answers here : get a recursive parent list (2 answers) Closed 5 years ago . I have the following tables: Category id INT name VARCHAR Category_parent category_id INT parent_id INT In Category_parent I store category relationship, and both columns get id from category table. So I can identify what is parent of what. There can be any number of generations, so it is a bit difficult to find all categories from which a particular category inherited. For example, CAT10's

Solving Caliban problems with prolog

我的未来我决定 提交于 2020-01-05 18:57:26
问题 I'm working on solving a logic puzzle using prolog for school. Here's the clues: Brown, Clark, Jones and Smith are 4 substantial citizens who serve their community as achitect, banker, doctor and lawyer, though not necessarily respectively. Brown, who is more conservative than Jones but more liberal than Smith, is a better golfer than the men who are younger than he is and has a larger income than the men who are older than Clark. The banker, who earns more than the architect, is neither the

Rails only give records that “belong_to”

拜拜、爱过 提交于 2020-01-05 12:57:36
问题 I have a author model, a article model and a metric model. Author has many articles and article has many metrics. No in my articles_controller 's show action I have @articles = Article.where(author_id: params[:id]) which limits the articles to the ones by that author. Can I do something similar for the metrics? Is there a rails way to say something like @metrics = Metric.WHICHBELONGSTO(@articles) ? Alternatively can I pass an array of allowed IDs in? like @metrics = Metric.where(article_id is

Yii correct syntax for conditions in relation definitions

余生颓废 提交于 2020-01-03 04:32:12
问题 I'm struggling to get the correct syntax for a relation condition I'm trying to set. The main relation is set with the foreign key question_id, but also contained in the child table is the user_id column. I wish to return only records related to the logged in user. Here's what I've got so far- class SurveyQuestion extends CActiveRecord { ....... public function relations() { return array( 'answered_questions' => array(self::HAS_MANY, 'AnsweredQuestion', 'question_id', 'condition'=>"answered