logo
down
shadow

Filter model, then join with many-to-many relation


Filter model, then join with many-to-many relation

By : Boris
Date : November 22 2020, 03:03 PM
it should still fix some issue You're close. Now we kind of need to mix the two queries you have and add a bit of whereHas
Try this (this code returns all the users who belong to the specified business unit and are also speakers on any training):
code :
$users = $this->user
         ->whereHas('trainings', function($q){
             $q->where('training_user.is_speaker', true);
         })
         ->where('business_unit', $business_unit)
         ->get();
$trainings = $this->training->whereHas( 'users', function( $q )
{
    $business_unit  = Input::get( 'bu' );
    $q->where( 'training_user.is_speaker', true );
    $q->where( 'business_unit', $business_unit );
})->get;
public function speakers(){
    return $this->belongsToMany('User')->wherePivot('is_speaker', true);
}
$business_unit = Input::get('bu');
$trainings = $this->training->has('speakers')->with('speakers')->get();
$trainings = $trainings->filter(function($training) use ($business_unit){
    return $training->speakers->sortBy('training_user.id')->first()->business_unit == $business_unit;
});


Share : facebook icon twitter icon
Get join model from has_many_through relation

Get join model from has_many_through relation


By : user2237944
Date : March 29 2020, 07:55 AM
will be helpful for those in need Either I'm not understanding your question or you're going to be surprised how obvious this is. You will have defined the relationship as so:
code :
#coffee_drinker.rb
has_many :cups
has_many :coffees, :through => :cup

#cup.rb
belongs_to :coffee
belongs_to :coffee_drinker

#coffee.rb
has_many :cups
has_many :coffee_drinkers, :through => :cup

coffee_drinker.cups
coffee_drinker.coffees

coffee.cups
coffee.coffee_drinkers

#coffee_drinker.rb
after_save :update_cups_drunk

def update_cups_drunk
  cups.find_by_coffee_id(coffee_id).increment!(:count)
end

#maybe you don't have access to the coffee_id

def update_cups_drunk
  cups.find_by_coffee_id(coffees.last.id).increment!(:count)
end
Yii Model Relation 'on' option to join the tables in yii Model relation

Yii Model Relation 'on' option to join the tables in yii Model relation


By : Yash
Date : March 29 2020, 07:55 AM
Any of those help What is going on with me on Yii model Relation ! I have 2 Models: , You have your relation setup incorrectly:
code :
'video'=>array(self::HAS_MANY, 'Video', 'question_id'),
//this compares Video->question_id to Question->primary_key
'question'=>array(self::BELONGS_TO,'Question','question_id'),
//this compares Video->question_id to Question->primary_key
echo CHtml::encode($data->video->video_title);
Many-To-Many relation with same model - 2 join tables

Many-To-Many relation with same model - 2 join tables


By : Jahangeer Akbar
Date : March 29 2020, 07:55 AM
Hope this helps See the following (looong) answer for a pretty comprehensive list of possible ways to do HABTM associations between objects of the same model: https://stackoverflow.com/a/2168528/454094
Once you decide on whether to use a "normal" join table or use the :through option with a model inbetween, defining two different HABTM associations shouldn't be more complicated than using different names and join tables / models for them. E.g.
code :
class Person < ActiveRecord::Base

  has_and_belongs_to_many(:friends,
    class_name: "Person",
    join_table: "friend_connections",
    foreign_key: "person_a_id",
    association_foreign_key: "person_b_id")

  has_and_belongs_to_many(:foes,
    class_name: "Person",
    join_table: "foes_connections",
    foreign_key: "person_a_id",
    association_foreign_key: "person_b_id")

end
Django model filter avoid join when filtering by relation id

Django model filter avoid join when filtering by relation id


By : Aadarsh Goswami
Date : March 29 2020, 07:55 AM
will be helpful for those in need The underlying db column is called category_id (with a single underscore); you can filter on that directly:
code :
projects = Project.objects.filter(category_id=1111)
Filter a join on each primary-foreign key relation only

Filter a join on each primary-foreign key relation only


By : user1531228
Date : March 29 2020, 07:55 AM
should help you out I am using MySql. , An example of a correlated sub query in a where clause
code :
drop table if exists t,t1;
create table t(id int);
create table t1(jid int,dt date);
insert into t values
(1),(2),(3);

insert into t1 values
(1,'2018-01-01'),
(1,'2018-02-01'),
(2,'2018-01-01'),
(3,'2018-01-01'),
(3,'2018-02-01'),
(3,'2018-03-01');

select t.id,t1.dt
from t
join t1 on t1.jid = t.id
where t1.dt =(select max(dt) from t1 where t1.jid = t.id);

+------+------------+
| id   | dt         |
+------+------------+
|    1 | 2018-02-01 |
|    2 | 2018-01-01 |
|    3 | 2018-03-01 |
+------+------------+
3 rows in set (0.00 sec)
select t.id,s.dt
from t
join 
(select t1.jid,t1.dt ,
          if(t1.jid<>@p,@rn:=1,@rn:=@rn+1) rn,
          @p:=t1.jid p
from t1 
cross join (select @rn:=0,@p:=0) r
order by t1.jid ,t1.dt desc
) s on s.jid = t.id
where s.rn <= 2;

+------+------------+
| id   | dt         |
+------+------------+
|    1 | 2018-01-01 |
|    1 | 2018-02-01 |
|    2 | 2018-01-01 |
|    3 | 2018-02-01 |
|    3 | 2018-03-01 |
+------+------------+
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org