小编Noe*_*ers的帖子

如何从has_many获取行数:通过与:uniq => true的关系

这是我的模特:

class Tag < ActiveRecord::Base
  # id, name
  has_many :taggings
end

class Tagging < ActiveRecord::Base
  # id, tag_id, owner_id, target_type, target_id
  belongs_to :tag
  belongs_to :owner, :class_name => 'User'
  belongs_to :target, :polymorphic => true
  validates_uniqueness_of :tag_id, :scope => [ :target_id, :target_type, :owner_id ]
end

class Asset < ActiveRecord::Base
  # id, owner_id, title, type, etc
  belongs_to :owner, :class_name => 'User'
  has_many :taggings, :as => :target
  has_many :taggers, :through => :taggings, :source => :owner, :uniq => true
  has_many :tags, :through => :taggings, :uniq …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails has-many-through

6
推荐指数
1
解决办法
1214
查看次数

使用has_many:through时如何访问rails连接模型属性

我有一个像这样的数据模型:

# columns include collection_item_id, collection_id, item_id, position, etc
class CollectionItem < ActiveRecord::Base
  self.primary_key = 'collection_item_id'
  belongs_to :collection
  belongs_to :item
end

class Item < ActiveRecord::Base
  has_many :collection_items
  has_many :collections, :through => :collection_items, :source => :collection
end

class Collection < ActiveRecord::Base
  has_many :collection_items, :order => :position
  has_many :items, :through => :collection_items, :source => :item, :order => :position
end
Run Code Online (Sandbox Code Playgroud)

项目可以出现在多个集合中,也可以在同一集合中的不同位置出现多次.

我正在尝试创建一个帮助方法,创建一个包含每个集合中每个项目的菜单.我想使用collection_item_id来跟踪请求之间当前选择的项目,但我无法通过Item类访问连接模型的任何属性.

def helper_method( collection_id )
  colls = Collection.find :all
  colls.each do |coll|
    coll.items.each do |item|
# !!! FAILS HERE ( undefined method …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails

5
推荐指数
1
解决办法
1万
查看次数

Javascript/xhtml - 使用overflow:hidden发现div中内容的总宽度

我正在编写一个基于javascript的照片库,其中包含一个可水平滚动的缩略图栏.

>>我目前正在进行的工作在这里<<

我希望缩略图栏在到达最后一个缩略图时停止滚动.要做到这一点,我需要找到div的内容的总宽度 - 最好不要添加所有缩略图和边距的宽度.

我在window.onload函数中添加了一个警报,因此我可以在不同的浏览器中测试各种元素维度函数.目前它显示了scrollWidth的值,由IE报告为1540px,FireFox,Safari,Opera等报告为920px.

值1540对我来说是正确的,任何人都可以告诉我如何在FireFox等中获取此值.

javascript css xhtml

1
推荐指数
1
解决办法
3985
查看次数