首页 \ 问答 \ 了解Rails关联(Understanding Rails associations)

了解Rails关联(Understanding Rails associations)

我正在学习Rails,我正在尝试将Ruby与创建关联时正在发生的事情联系起来。 例如:

class Post < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :posts
end

我在网上阅读了一篇解释,其中将belongs_to和has_many的使用与Ruby中的attr_accessor联系起来。 这有点令人困惑......怎么会这样? 据我所知,这设置了Post和User之间的1:M关联,特别是Post有一个包含用户ID的外键。 在rails控制台中,我可以执行以下操作:

user = User.first
user.posts 
user2 = User.create(username: 'some guy').save
post2 = Post.new(title: 'stuff', body: 'some more stuff')
user2.posts << post2 

那些类似'getter'和'setter'的方法,其中每个类的对象对应于数据库中的特定行,我可以使用这些方法,因为它们的关联/关系?


I'm learning Rails and I'm trying to connect the dots between Ruby and what's going on when creating associations. For example:

class Post < ActiveRecord::Base
  belongs_to :user
end

class User < ActiveRecord::Base
  has_many :posts
end

I've read an explanation online that relates the use of belongs_to and has_many here to attr_accessor in Ruby. This is a tad confusing ... how is that so? I understand this sets up the 1:M association between Post and User, specifically Post has a foreign key containing a user id. In the rails console, I can do something like:

user = User.first
user.posts 
user2 = User.create(username: 'some guy').save
post2 = Post.new(title: 'stuff', body: 'some more stuff')
user2.posts << post2 

So are these kind of like 'getter' and 'setter' methods where an object of each class corresponds to a specific row in the database and I can use these methods because of their association/relationship?


原文:https://stackoverflow.com/questions/27498071
更新时间:2024-01-09 06:01

最满意答案

您还可以为您的块创建一个physicsBody而不给这些块阻止动态属性设置为true(false =静态对象),因此对于每个块:

block.physicsBody = SKPhysicsBody.init(edgeLoopFromRect: block.frame)
block.physicsBody?.mass = 1
block.physicsBody?.affectedByGravity = false
block.physicsBody?.restitution = 1
block.physicsBody?.dynamic = false
block.physicsBody?.usesPreciseCollisionDetection = true
block.physicsBody?.categoryBitMask = CollisionTypes.Block.rawValue
block.physicsBody?.contactTestBitMask = CollisionTypes. PlayerObject.rawValue

关于你的CollisionTypes可能是:

enum CollisionTypes: UInt32 {
    case Block = 1
    case PlayerObject = 2
}

添加到你的playerObject.physicsBody?.contactTestBitMask也是块,并检查块和播放器之间的联系直接到didBeginContact


You could also make a physicsBody to your blocks without give to these block the dynamic property set to true (false = static object),so for each block:

block.physicsBody = SKPhysicsBody.init(edgeLoopFromRect: block.frame)
block.physicsBody?.mass = 1
block.physicsBody?.affectedByGravity = false
block.physicsBody?.restitution = 1
block.physicsBody?.dynamic = false
block.physicsBody?.usesPreciseCollisionDetection = true
block.physicsBody?.categoryBitMask = CollisionTypes.Block.rawValue
block.physicsBody?.contactTestBitMask = CollisionTypes. PlayerObject.rawValue

About your CollisionTypes could be:

enum CollisionTypes: UInt32 {
    case Block = 1
    case PlayerObject = 2
}

Add to your playerObject.physicsBody?.contactTestBitMask also the block , and check for the contacts between blocks and player directly to didBeginContact

相关问答

更多
  • 从任意一个点开始说A.现在拿第二个点说B,并将它绘制在以A为中心并且半径为A和B之间的圆上的某处。现在再取一个点C.设距离(A,C)=x和(B,C)=y 。 找出圆的交点(A,x)和(B,y) 。 标记为C 圆(P,q)指定中心在P和半径q 。 如果不存在这样的点,那么给定的数据是不正确的。 现在取第4 个点,同样找出第一个三点中心与第四个点与其他三个点之间的距离为半径的交点。 应用此方法,直到绘制所有点。 请注意,RobH指出可以有无数的解决方案。 既然你只需要一个虚拟表示,我想任何有效的解决方案都足够了 ...
  • 子节点将是直接在某个节点(父节点)下面的节点。 后代将是在同一节点“下面”的节点,但它也可以是子节点“下面”的多个级别。 子节点将是后代,但后代不会始终是子节点。 Eg: -Parent | --> Child1 (Descendant) | --> Child2 (Descendant) | --> Anoth ...
  • 路径是顶点序列: s, v_1, .., v_m, t 。 如果对于任何有效的i和j v_i != u_j ,则两个路径s, v_1, .., v_m, t和s, u_1, ..., u_k, t被称为节点不相交。 我们可以通过将每个顶点(源和目标除外)分成两个,从第一个副本到第二个副本添加边,重定向以此结尾的所有边来减少此问题,以找到最大数量的边不相交路径顶点到第一个副本和第二个副本的所有传出边。 之后,答案是此图表中的最大流量(所有边缘应具有单位容量)。 A path is sequence of ve ...
  • 您还可以为您的块创建一个physicsBody而不给这些块阻止动态属性设置为true(false =静态对象),因此对于每个块: block.physicsBody = SKPhysicsBody.init(edgeLoopFromRect: block.frame) block.physicsBody?.mass = 1 block.physicsBody?.affectedByGravity = false block.physicsBody?.restitution = 1 block.physics ...
  • 只要它们实现了CCRGBAProtocol就可以对受影响的节点 - 精灵,标签等进行着色 。 例如,这三种颜色可用于白天,黎明和夜晚: // full brightness sprite.color = ccc3(255, 255, 255); // getting darker sprite.color = ccc3(200, 200, 220); // night sprite.color = ccc3(140, 140, 180); 我拉起蓝色通道,因为蓝色仍然感觉非常黑暗,而不会消耗太多的亮度。 ...
  • 当然不简单。 一种解决方案是将所有形状彼此略微间隔开来。 向所有六个边添加具有物理实体的不可见子节点,并根据其等级(1,2或3)为每个物理主体提供适当的类别。 当您开始游戏时,使用您喜欢的任何移动方法将所有外部节点移动到正确的位置(侧面接触)。 这将为您提供联系人消息,因为每个六边形接触另一个。 联系人消息将告诉您哪个边号正在触及其邻居。 这个想法的确切编码取决于您当前的代码,游戏等... Certainly not simple. One solution would be to start all yo ...
  • 我假设你在谈论力量布局。 你无法直接做你想做的事,但你可以用几种力布局来做。 对于来自不同组的每对节点,您需要创建一个单独的力布局,使它们相互排斥。 您最终会得到二次数的力布局,但除非您计划拥有多个节点(或更多组),否则它在性能方面不应成为问题。 I'm assuming that you are talking about the force layout. You can't do what you want directly, but you can do it with several force ...
  • 这个答案扩展了@Stefan的原始答案,以返回所有类别的结果,而不仅仅是其中一个。 START p = node(1) MATCH (p)-->(category)<--(m) WITH category, labels(m) as label, collect(m)[0..2] as nodes UNWIND label as lbl UNWIND nodes AS n RETURN category, lbl, n 为了便于手动验证结果,还可以将此行添加到结尾,以对结果进行排序。 (这种排序可能不应 ...
  • 当我尝试将静态节点(位于左侧)与具有旋转的节点(以及从右侧飞行)相交时,我遇到了同样的问题。 我这样解决了 if ([ninja intersectsNode:node] && CGRectGetMinX(node.frame) <= CGRectGetMaxX(ninja.frame) && CGRectGetMaxX(node.frame) >= CGRectGetMinX(ninja.frame)) { //and here I have intersects } 所以我通过 ...
  • 您可以从路径中展开节点集合,然后返回不同的节点。 MATCH(t:THING1 {id:"t1"})-[:AFFECTS]->(x:SOME_NODE), (t)-[:CHANGES]->(SOME_NODE) MATCH p=shortestpath((s)-[MY_RELATION*0..4]->(x)) UNWIND nodes(p) as n RETURN DISTINCT n You could just unwind the collections of nodes from the path ...

相关文章

更多

最新问答

更多
  • h2元素推动其他h2和div。(h2 element pushing other h2 and div down. two divs, two headers, and they're wrapped within a parent div)
  • 创建一个功能(Create a function)
  • 我投了份简历,是电脑编程方面的学徒,面试时说要培训三个月,前面
  • PDO语句不显示获取的结果(PDOstatement not displaying fetched results)
  • Qt冻结循环的原因?(Qt freezing cause of the loop?)
  • TableView重复youtube-api结果(TableView Repeating youtube-api result)
  • 如何使用自由职业者帐户登录我的php网站?(How can I login into my php website using freelancer account? [closed])
  • SQL Server 2014版本支持的最大数据库数(Maximum number of databases supported by SQL Server 2014 editions)
  • 我如何获得DynamicJasper 3.1.2(或更高版本)的Maven仓库?(How do I get the maven repository for DynamicJasper 3.1.2 (or higher)?)
  • 以编程方式创建UITableView(Creating a UITableView Programmatically)
  • 如何打破按钮上的生命周期循环(How to break do-while loop on button)
  • C#使用EF访问MVC上的部分类的自定义属性(C# access custom attributes of a partial class on MVC with EF)
  • 如何获得facebook app的publish_stream权限?(How to get publish_stream permissions for facebook app?)
  • 如何防止调用冗余函数的postgres视图(how to prevent postgres views calling redundant functions)
  • Sql Server在欧洲获取当前日期时间(Sql Server get current date time in Europe)
  • 设置kotlin扩展名(Setting a kotlin extension)
  • 如何并排放置两个元件?(How to position two elements side by side?)
  • 如何在vim中启用python3?(How to enable python3 in vim?)
  • 在MySQL和/或多列中使用多个表用于Rails应用程序(Using multiple tables in MySQL and/or multiple columns for a Rails application)
  • 如何隐藏谷歌地图上的登录按钮?(How to hide the Sign in button from Google maps?)
  • Mysql左连接旋转90°表(Mysql Left join rotate 90° table)
  • dedecms如何安装?
  • 在哪儿学计算机最好?
  • 学php哪个的书 最好,本人菜鸟
  • 触摸时不要突出显示表格视图行(Do not highlight table view row when touched)
  • 如何覆盖错误堆栈getter(How to override Error stack getter)
  • 带有ImageMagick和许多图像的GIF动画(GIF animation with ImageMagick and many images)
  • USSD INTERFACE - > java web应用程序通信(USSD INTERFACE -> java web app communication)
  • 电脑高中毕业学习去哪里培训
  • 正则表达式验证SMTP响应(Regex to validate SMTP Responses)