了解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
最满意答案
您还可以为您的块创建一个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 todidBeginContact
相关问答
更多-
从任意一个点开始说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 ... -
什么是节点不相交的路径?(What is node-disjoint paths?)[2022-03-23]
路径是顶点序列: 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 ... -
相交节点(Intersecting Nodes)[2023-09-09]
您还可以为您的块创建一个physicsBody而不给这些块阻止动态属性设置为true(false =静态对象),因此对于每个块: block.physicsBody = SKPhysicsBody.init(edgeLoopFromRect: block.frame) block.physicsBody?.mass = 1 block.physicsBody?.affectedByGravity = false block.physicsBody?.restitution = 1 block.physics ... -
如何着色节点?(How to tint nodes?)[2023-05-24]
只要它们实现了CCRGBAProtocol就可以对受影响的节点 - 精灵,标签等进行着色 。 例如,这三种颜色可用于白天,黎明和夜晚: // full brightness sprite.color = ccc3(255, 255, 255); // getting darker sprite.color = ccc3(200, 200, 220); // night sprite.color = ccc3(140, 140, 180); 我拉起蓝色通道,因为蓝色仍然感觉非常黑暗,而不会消耗太多的亮度。 ... -
如何检测其他SKSpriteNodes的子节点的交叉节点(How to detect intersecting nodes of children of other SKSpriteNodes)[2022-01-09]
当然不简单。 一种解决方案是将所有形状彼此略微间隔开来。 向所有六个边添加具有物理实体的不可见子节点,并根据其等级(1,2或3)为每个物理主体提供适当的类别。 当您开始游戏时,使用您喜欢的任何移动方法将所有外部节点移动到正确的位置(侧面接触)。 这将为您提供联系人消息,因为每个六边形接触另一个。 联系人消息将告诉您哪个边号正在触及其邻居。 这个想法的确切编码取决于您当前的代码,游戏等... Certainly not simple. One solution would be to start all yo ... -
只排斥不同组的节点(Only repel nodes of different group)[2023-07-31]
我假设你在谈论力量布局。 你无法直接做你想做的事,但你可以用几种力布局来做。 对于来自不同组的每对节点,您需要创建一个单独的力布局,使它们相互排斥。 您最终会得到二次数的力布局,但除非您计划拥有多个节点(或更多组),否则它在性能方面不应成为问题。 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 ... -
限制每个标签的节点(Limiting nodes per label)[2022-03-11]
这个答案扩展了@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 } 所以我通过 ...
-
从路径获取节点(Getting nodes from paths)[2022-07-11]
您可以从路径中展开节点集合,然后返回不同的节点。 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 ...