Rails - 使用两个条件从表中提取属性值

时间:2016-01-30 22:23:33

标签: ruby-on-rails database sqlite model-view-controller querying

我的rails应用程序中有两个表:用户表和活动表。用户有很多活动。现在,我试图找出如何打印活动名称,其中activity_type =:personal AND where user_id = current_user.id。

在我的活动索引文件中,我有以下内容:

Ext.application({
    name : 'Fiddle',

    launch : function() {
        Ext.create('Ext.form.Panel', {
            title: 'On The Wall',
            width: 300,
            bodyPadding: 10,
            renderTo: Ext.getBody(),
            items: [{
                xtype: 'numberfield',
                anchor: '100%',
                name: 'bottles',
                fieldLabel: 'Bottles of Beer',
                value: 99,
                maxValue: 99,
                minValue: 0,
                allowExponential: false,
                allowDecimals: true
            }],
            buttons: [{
                text: 'Take one down, pass it around',
                handler: function() {
                    this.up('form').down('[name=bottles]').spinDown();
                }
            }]
        });
    }
});

页面上没有打印出来 - 我肯定已将用户和活动添加到数据库中,因为我已经检查了rails consoler。我是否错误地设置了每个do循环?有没有不同的方法来查询这些数据?

2 个答案:

答案 0 :(得分:0)

应该这样写:

<% @activities.where(user_id: current_user.id, activity_type: "personal").each do |activity| %>
  <%= activity.activity_name %>
<% end %>

或者更好,因为用户has_many活动:

<% current_user.activities.where(activity_type: "personal").each do |activity| %>
  <%= activity.activity_name %>
<% end %>

我们可以使用范围进一步压缩它:

class Activity < ActiveRecord::Base
  def self.personal
    where(activity_type: "personal")
  end
end

在控制器中:

def index # or whatever you wnat
  @activities = current_user.activities.personal
end

观点:

<% @activities.each do |activity| %>
  <%= activity.activity_name %>
<% end %>

答案 1 :(得分:0)

如果您有用户的活动关联,您应该可以执行类似

的操作
ShapeData ObjectGenerator::makeAngularSpringMass(glm::vec3 anchorPosition, GLfloat springWidth, GLfloat stretch, GLfloat width, GLfloat height, GLfloat angle) {
    ShapeData ret;
    angle = glm::radians(angle);
    glm::vec3 springColor{ 1.0f, 0.0f, 0.0f }, massColor{ 0.0f, 1.0f, 0.0f }, connectionPoint{ anchorPosition.x, anchorPosition.y - (17 * stretch), 0.0f };

    //The original model
    Vertex springMass[] = {
        glm::vec3(anchorPosition.x, anchorPosition.y, 0.0f), // 0
        springColor,
        glm::vec3(anchorPosition.x, anchorPosition.y - (0.1 *stretch),0.0f), // 1
        springColor,
        glm::vec3(anchorPosition.x + springWidth, anchorPosition.y - (1 * stretch), 0.0f), // 2
        springColor,
        glm::vec3(anchorPosition.x - springWidth, anchorPosition.y - (3 * stretch), 0.0f), // 3
        springColor,
        glm::vec3(anchorPosition.x + springWidth, anchorPosition.y - (5 * stretch), 0.0f), // 4
        springColor,
        glm::vec3(anchorPosition.x - springWidth, anchorPosition.y - (7 * stretch), 0.0f), // 5
        springColor,
        glm::vec3(anchorPosition.x + springWidth, anchorPosition.y - (9 * stretch), 0.0f), // 6
        springColor,
        glm::vec3(anchorPosition.x - springWidth, anchorPosition.y - (11 * stretch), 0.0f), // 7
        springColor,
        glm::vec3(anchorPosition.x + springWidth, anchorPosition.y - (13 * stretch), 0.0f), // 8
        springColor,
        glm::vec3(anchorPosition.x - springWidth, anchorPosition.y - (15 * stretch), 0.0f), // 9
        springColor,
        glm::vec3(anchorPosition.x, anchorPosition.y - (16 * stretch), 0.0f), // 10
        springColor,
        connectionPoint, // 11
        springColor,
        //=================Mass==============//
        glm::vec3(connectionPoint.x - width, connectionPoint.y, 0.0f), //top Left 12
        massColor,
        glm::vec3(connectionPoint.x + width, connectionPoint.y, 0.0f), //top Right 13
        massColor,
        glm::vec3(connectionPoint.x + width, connectionPoint.y - height, 0.0f), // bottom right 14
        massColor,
        glm::vec3(connectionPoint.x - width, connectionPoint.y - height, 0.0f), // bottom left 15
        massColor,
    };

    //New vertices recomputed around anchorPosition
    Vertex vertices[] = {
        glm::vec3(anchorPosition.x, anchorPosition.y, 0.0f), // 0
        springColor,
        glm::vec3(getRotatedX(angle, springMass[1], anchorPosition),  getRotatedY(angle, springMass[1], anchorPosition), 0.0f), // 1
        springColor,
        glm::vec3(getRotatedX(angle, springMass[2], anchorPosition),  getRotatedY(angle, springMass[2], anchorPosition), 0.0f), // 2
        springColor,
        glm::vec3(getRotatedX(angle, springMass[3], anchorPosition),  getRotatedY(angle, springMass[3], anchorPosition), 0.0f), // 3
        springColor,
        glm::vec3(getRotatedX(angle, springMass[4], anchorPosition),  getRotatedY(angle, springMass[4], anchorPosition), 0.0f), // 4
        springColor,
        glm::vec3(getRotatedX(angle, springMass[5], anchorPosition),  getRotatedY(angle, springMass[5], anchorPosition), 0.0f), // 5
        springColor,
        glm::vec3(getRotatedX(angle, springMass[6], anchorPosition),  getRotatedY(angle, springMass[6], anchorPosition), 0.0f), // 6
        springColor,
        glm::vec3(getRotatedX(angle, springMass[7], anchorPosition),  getRotatedY(angle, springMass[7], anchorPosition), 0.0f), // 7
        springColor,
        glm::vec3(getRotatedX(angle, springMass[8], anchorPosition),  getRotatedY(angle, springMass[8], anchorPosition), 0.0f), // 8
        springColor,
        glm::vec3(getRotatedX(angle, springMass[9], anchorPosition),  getRotatedY(angle, springMass[9], anchorPosition), 0.0f), // 9
        springColor,
        glm::vec3(getRotatedX(angle, springMass[10], anchorPosition),  getRotatedY(angle, springMass[10], anchorPosition), 0.0f), // 10
        springColor,
        glm::vec3(getRotatedX(angle, springMass[11], anchorPosition),  getRotatedY(angle, springMass[11], anchorPosition), 0.0f), // 11
        springColor,
        //=================Mass==============//
        glm::vec3(getRotatedX(angle, springMass[12], anchorPosition),  getRotatedY(angle, springMass[12], anchorPosition), 0.0f), // 12
        massColor,
        glm::vec3(getRotatedX(angle, springMass[13], anchorPosition),  getRotatedY(angle, springMass[13], anchorPosition), 0.0f), // 13
        massColor,
        glm::vec3(getRotatedX(angle, springMass[14], anchorPosition),  getRotatedY(angle, springMass[14], anchorPosition), 0.0f), // 14
        massColor,
        glm::vec3(getRotatedX(angle, springMass[15], anchorPosition),  getRotatedY(angle, springMass[15], anchorPosition), 0.0f), // 15
        massColor,
    };
    ret.numVertices = NUM_ARRAY_ELEMENTS(vertices);
    ret.vertices = new Vertex[ret.numVertices];
    memcpy(ret.vertices, vertices, sizeof(vertices)); //memcpy(dest, source, size);
    GLushort indices[] = { 0,1 ,1,2, 2,3, 3,4, 4,5, 5,6, 6,7, 7,8, 8,9, 9,10, 10,11, 11,12, 12,13, 13,14, 14,15, 15,12 };
    ret.numIndices = NUM_ARRAY_ELEMENTS(indices);
    ret.indices = new GLushort[ret.numIndices];
    ret.connectionPoint = connectionPoint;
    memcpy(ret.indices, indices, sizeof(indices));

    return ret;
}

GLfloat ObjectGenerator::getRotatedX(GLfloat angle, Vertex old, glm::vec3 anchorPosition) {
    GLfloat newX = (glm::cos(angle) * (old.position.x - anchorPosition.x)) - (glm::sin(angle) * (old.position.y - anchorPosition.y)) + anchorPosition.x;
    return newX;
}
GLfloat ObjectGenerator::getRotatedY(GLfloat angle, Vertex old, glm::vec3 anchorPosition) {
    GLfloat newY = (glm::sin(angle) * (old.position.x - anchorPosition.x)) - (glm::cos(angle) * (old.position.y - anchorPosition.y)) + anchorPosition.y;
    return newY;
}