首页 \ 问答 \ 属于_通过协会(belongs_to through associations)

属于_通过协会(belongs_to through associations)

鉴于以下关联,我需要参考Choice所附的Question 。 我一直在尝试使用belongs_to :question, through: :answer来执行此操作。

class User
  has_many :questions
  has_many :choices
end

class Question
  belongs_to :user
  has_many :answers
  has_one :choice, :through => :answer
end

class Answer
  belongs_to :question
end

class Choice
  belongs_to :user
  belongs_to :answer
  belongs_to :question, :through => :answer

  validates_uniqueness_of :answer_id, :scope => [ :question_id, :user_id ]
end

我正进入(状态

NameError未初始化常量User::Choice

当我尝试做current_user.choices

它工作正常,如果我不包括

belongs_to :question, :through => :answer

但是我想使用它,因为我想要能够执行validates_uniqueness_of

我可能忽略了一些简单的东西。 任何帮助将不胜感激。


Given the following associations, I need to reference the Question that a Choice is attached through from the Choice model. I have been attempting to use belongs_to :question, through: :answer to perform this action.

class User
  has_many :questions
  has_many :choices
end

class Question
  belongs_to :user
  has_many :answers
  has_one :choice, :through => :answer
end

class Answer
  belongs_to :question
end

class Choice
  belongs_to :user
  belongs_to :answer
  belongs_to :question, :through => :answer

  validates_uniqueness_of :answer_id, :scope => [ :question_id, :user_id ]
end

I am getting

NameError uninitialized constant User::Choice

when I try to do current_user.choices

It works fine, if I don't include the

belongs_to :question, :through => :answer

But I want to use that because I want to be able to do the validates_uniqueness_of

I am probably overlooking something simple. Any help would be appreciated.


原文:https://stackoverflow.com/questions/4021322
更新时间:2023-03-16 21:03

最满意答案

通常我使用.itertext方法来提取文本:

from lxml.html import fromstring

def extract_text(el, sep=' '):
    return sep.join(s.strip() for s in li.itertext() if s.strip())

tree = fromstring(html)
for li in tree.cssselect('.elems li'):
    print(extract_text(li))

The easiest solution ever. Values are within "li" tag not "b".

from lxml import html as e

root = e.fromstring(html)
for items in root.cssselect(".elems"):
    item = [item.text_content() for item in items.cssselect("li")]
    print(''.join(item))

相关问答

更多
  • 如果要使用nth-child选择器,你需要绕过input ,因为它计算所有孩子,无论他们的类型 第nth-child(4n+4)从第4个元素(你的第二个标签)开始,然后计数到每个第4个并应用规则,在你的情况下将是每个偶数标签 nth-child(4n+2)从2:nd元素(您的第一个标签)开始,然后计数到每4:并应用规则,在您的情况下将是每个奇数标签。 注意,也可以使用nth-child(4n)代替nth-child(4n+4) ,它将从0:th元素(不存在)开始,然后计数到每4:th。 .rating { ...
  • 不,但将其作为插件添加将是微不足道的: $.expr[':'].contentIs = function(el, idx, meta) { return $(el).text() === meta[3]; }; 然后你可以使用它作为$('a:contentIs("Baxter")') No, but it would be trivial to add as a plugin: $.expr[':'].contentIs = function(el, idx, meta) { return ...
  • 通常我使用.itertext方法来提取文本: from lxml.html import fromstring def extract_text(el, sep=' '): return sep.join(s.strip() for s in li.itertext() if s.strip()) tree = fromstring(html) for li in tree.cssselect('.elems li'): print(extract_text(li)) The easi ...
  • :after选择器:after使用top,left,right,bottom值。 演示: http : //jsfiddle.net/lotusgodkk/w1v07drv/1/ CSS: .text-bg .text-top-left { position:absolute; width:50%; background:rgba(95, 87, 79, 0.9); padding:20px; box-sizing:border-box; -webkit-bo ...
  • 我们不再使用content元素。 至于RC,我们使用slot元素。 也就是说, slot元素不支持ref绑定,因为slot元素有点“消失”并被替换为正在投影的内容。 这是因为在Shadow DOM规范中, slot s实际上不是元素,它们是'处理指令'。 We no longer use the content element. As of the RC, we use the slot element. That being said, the slot element doesn't support t ...
  • 干得好: $(function() { $("input[name='add'] .my").click(function(){ var parent = $(this).parent().parent().parent(); //OR var parent = $(this).closest('div'); //do stuff... }); }); Here you go: $(function() { $("input[name='a ...
  • var s = $('#foo span'); $('#foo').text("hello").append(s); 演示: http : //jsfiddle.net/uTNTF/ 或者,如果更新HTML是一个选项,那么简单地将yyy包装在将使您的生活变得更简单: $('#foo span:first-child').text("hello"); 演示: http : //jsfiddle.net/uTNTF/1/ var s = $('#foo span'); $('#foo').tex ...
  • :not()选择器按预期工作。 问题是你的包装器div有没有collapsed的content类,然后你有一个collapsed的类。 删除第一个div使其按预期工作。 http://jsfiddle.net/3L7ym/
  • 不,没有。 如果CSS将有一个内容选择器,它将保留:contains() 。 对于它的价值, jQuery实现:contains() ,所以如果你已经使用它,你可以用它来应用样式。 No, there isn't. If CSS was going to have a content selector, it would have kept :contains(). For what it's worth, jQuery implements :contains(), so if you're using ...
  • 它们是有区别的, 演示:多个选择器 var $fullContent = $('#full-sized-content, #full-sized-area'); 选择两个元素 - 多个选择器 : $('#elem1,#elem2,...') 演示:亲子 var $fullContent = $('#full-sized-content', $fullArea); 在这种情况下: #full-sized-content必须是#full-sized-area的子#full-sized-area 所以它相当于 ...

相关文章

更多

最新问答

更多
  • python的访问器方法有哪些
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。
  • 响应navi重叠h1和nav上的h1链接不起作用(Responsive navi overlaps h1 and navi links on h1 isn't working)
  • 在C中读取文件:“r”和“a +”标志的不同行为(Reading a File in C: different behavior for “r” and “a+” flags)
  • NFC提供什么样的带宽?(What Kind of Bandwidth does NFC Provide?)
  • 元素上的盒子阴影行为(box-shadow behaviour on elements)
  • Laravel检查是否存在记录(Laravel Checking If a Record Exists)
  • 设置base64图像的大小javascript - angularjs(set size of a base64 image javascript - angularjs)
  • 想学Linux 运维 深圳有哪个培训机构好一点
  • 为什么有时不需要在lambda中捕获一个常量变量?(Why is a const variable sometimes not required to be captured in a lambda?)
  • 在Framework 3.5中使用服务器标签<%=%>设置Visible属性(Set Visible property with server tag <%= %> in Framework 3.5)
  • AdoNetAppender中的log4net连接类型无效(log4net connection type invalid in AdoNetAppender)
  • 错误:发送后无法设置标题。(Error: Can't set headers after they are sent. authentication system)
  • 等待EC2实例重启(Wait for an EC2 instance to reboot)
  • 如何在红宝石中使用正则表达式?(How to do this in regex in ruby?)
  • 使用鼠标在OpenGL GLUT中绘制多边形(Draw a polygon in OpenGL GLUT with mouse)
  • 江民杀毒软件的KSysnon.sys模块是什么东西?
  • 处理器在传递到add_xpath()或add_value()时调用了什么顺序?(What order are processors called when passed into add_xpath() or add_value()?)
  • sp_updatestats是否导致SQL Server 2005中无法访问表?(Does sp_updatestats cause tables to be inaccessible in SQL Server 2005?)
  • 如何创建一个可以与持续运行的服务交互的CLI,类似于MySQL的shell?(How to create a CLI that can interact with a continuously running service, similar to MySQL's shell?)
  • AESGCM解密失败的MAC(AESGCM decryption failing with MAC)
  • SQL查询,其中字段不包含$ x(SQL Query Where Field DOES NOT Contain $x)
  • PerSession与PerCall(PerSession vs. PerCall)
  • C#:有两个构造函数的对象:如何限制哪些属性设置在一起?(C#: Object having two constructors: how to limit which properties are set together?)
  • 平衡一个精灵(Balancing a sprite)
  • n2cms Asp.net在“文件”菜单上给出错误(文件管理器)(n2cms Asp.net give error on Files menu (File Manager))
  • Zurb Foundation 4 - 嵌套网格对齐问题(Zurb Foundation 4 - Nested grid alignment issues)
  • 湖北京山哪里有修平板计算机的