从连接表访问数据

时间:2017-11-24 15:06:05

标签: mysql ruby-on-rails ruby

我想选择一个用户组,其中联接表标有User

Groups UsersGroup通过名为UsersGroup的联接表group_iduser_id模型包含变量favoritcurrent_user.groups.joins(:users_group).where(:users_group => { :favorit => true }) ,但也包含变量current_user.groups.where(users_group.favorit == true)

我想打电话给我的用户的偏好组。但我不知道如何写它。

我尝试过这样的事情:

public function __toString() 
{
    return (string) $this->name; 
}

&安培;

var btnWrite = document.getElementById("writeElement");
var btnStop = document.getElementById("stopElement");
var btnClear = document.getElementById("clearElement");
var btnSave = document.getElementById("saveElement");
		
btnWrite.addEventListener("click", writeData);
btnStop.addEventListener("click", stopText);
btnClear.addEventListener("click", clearText);
btnSave.addEventListener("click", saveText);

textareaStatus = document.body.appendChild( document.createElement( 'textarea' ) );

var timer = null; 

function writeData() {
    var text = ((0.1111111111*10).toFixed(6) + " " + (0.2222222222*10).toFixed(6) + " " + (0.3333333333*10).toFixed(6) + " " + (0.4444444444*10).toFixed(6) + " " + (0.5555555555*10).toFixed(6) + " " + (0.6666666666*10).toFixed(6) + " " + (0.7777777777*10).toFixed(6) +" "+ (0.8888888888*10).toFixed(6) + " " + (0.9999999999*10).toFixed(6) + "\n");
    timer = setTimeout(writeData, 1000.000);
    textareaStatus.value += text;

/*
    var path = "c:\\Temp\\Test.txt";
    var data = text;
			
    WriteFile(path, data, function(error) {
        if (error) {
            console.error("write error:  " + error.message);
        } else {
            console.log("Successful Write to " + path);
        }
    });
*/					
}

function stopText() {
    clearTimeout(timer);
    LineCounter();				
}		

function clearText() {
    frames = 0;
    textareaStatus.value = "";
    LineCounter() == "";
}

function saveText() {
    var txt = exampleFile;  //this file is available with me. 
    txt += textareaStatus.value; 
    var blob = new Blob( [ txt ] , {type: "text/plain;charset=utf-8"} );
    saveAs( blob, "File000.txt" );		
}	

function LineCounter() {			
    var lineBreaksCount = (textareaStatus.value.split('\n'));
    document.getElementById("LineCounter").innerHTML = (lineBreaksCount.length)-1;
}

您是否有针对此类请求的解决方案?

1 个答案:

答案 0 :(得分:1)

您可以使用:

current_user.user_groups.where(favorit: true)

如果您想从中访问这些群组,可以从上面找到的group获取user_groups。即。

favorits = current_user.user_groups.where(favorit: true).includes(:group)
favorits.map(&:group)

注意includes在那里急切加载群组并避免任何N + 1问题。

这会做你想要的吗?