我目前正在开发一个小型社交网络应用程序,现在我正在尝试创建一个代表用户之间友谊的模型.这是我到目前为止提出的:
class User < ActiveRecord::Base
# ...
has_many :friendships
has_many :friends, :through => :friendships
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => 'User'
end
Run Code Online (Sandbox Code Playgroud)
我的友谊模型有一个确认为布尔值的字段,我想用它来定义一个待定或确认的友谊.
如何访问特定用户的所有待处理请求?我可以使用Rails的范围方法以某种方式定义它吗?就像是
current_user.friendships.requests # => [Friendship, Friendship, ...]
Run Code Online (Sandbox Code Playgroud)
会很好.
如何使这种关联双向化?一旦确认了朋友请求,我就会添加另一个友谊,这样我的友谊表看起来就像这样:
| user_id | friend_id | confirmed |
-----------------------------------
| 1 | 2 | true |
| 2 | 1 | true |
Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的数据模型情况,所以也许我的整个方法是错误的.这是我正在做的事情:
我有一个名为Bird的类和一个名为Color的简单类.从概念上讲,每只鸟有两种与颜色相关的关联,一种用于男性颜色,一种用于女性颜色.我处理这个的方法是使用一个名为BirdColoration的连接模型,它属于一个鸟和一个颜色,并有一个额外的布尔字段来判断颜色是针对男性还是女性.因此,每只鸟实际上都与BirdColoration有着很好的关系,而且它通过BirdColoration与Color-toyman相关.如果这听起来合理,那么继续阅读.否则,停下来告诉我为什么这是错的!
我需要能够将鸟桌作为json转储.以前,当每只鸟只有一个与颜色有关联时,我可以使用:include来包含每个鸟的颜色在json转储中.现在,我将BirdColorations包括在转储中,但我仍然需要自己使用颜色模型.我可以单独包括每只鸟的颜色和颜色,然后在解析时匹配它们,但我更愿意直接包括每种颜色的颜色.就像是
format.json { render :json => @birds.to_json(:include => [{:bird_colorations => :color}, :seasons, :habitats, :image_holders]) }
Run Code Online (Sandbox Code Playgroud)
但是,上述方法不起作用.我认为这应该是可能的.任何人都可以指出我如何处理这个问题的正确方向?
现在,我将分别包括每只鸟的颜色和颜色,并在解析时匹配它们.至少我知道这会奏效.
谢谢!
我在rails中有很多关系.所有数据库表都相应地进行相应命名.所有模型文件都是复数,并使用下划线分隔单词.所有命名通知都遵循ruby和rails标准.我在我的模型中使用了很多这样的:
has_many :users, :through => :users_posts #Post model
has_many :posts, :through => :users_posts #User model
belongs_to :users #UsersSource model
belongs_to :posts #UsersSource model
Run Code Online (Sandbox Code Playgroud)
这个错误还有什么呢?
ActiveRecord::HasManyThroughAssociationNotFoundError in UsersController#welcome
Could not find the association :users_posts in model Post
activerecord many-to-many ruby-on-rails associations has-many-through
问候,
我有一个应用程序,其中Companies和Users需要通过一个CompanyMembership模型相互拥有,该模型包含有关成员资格的额外信息(具体来说,用户是否是公司的管理员,通过布尔值admin).一个简单的代码版本:
class CompanyMembership < ActiveRecord::Base
belongs_to :company
belongs_to :user
end
class Company < ActiveRecord::Base
has_many :company_memberships
has_many :users, :through => :company_memberships
end
class User < ActiveRecord::Base
has_many :company_memberships
has_many :companies, :through => :company_memberships
end
Run Code Online (Sandbox Code Playgroud)
当然,这使得通过公司company.users.all等所有成员获得简单.但是,我正在尝试获取公司中所有用户的列表,这些用户是该公司的管理员(并且还要测试用户是否是给定公司的管理员).我的第一个解决方案如下company.rb:
def admins
company_memberships.where(:admin => true).collect do |membership|
membership.user
end
end
def is_admin?(user)
admins.include? user
end
Run Code Online (Sandbox Code Playgroud)
虽然这有效,但它的效率却很低(它会迭代每个成员资格,每次都执行SQL,对吗?或者是比这更聪明的关系吗?),我不确定是否有更好的方法来解决这个问题(可能使用范围或RelationRails 3使用的花哨的新对象?).
任何关于最佳处理方式的建议(最好使用Rails 3最佳实践)将不胜感激!
我正在尝试解决一个非常常见的(我认为)任务.
有三种型号:
class Product < ActiveRecord::Base
validates :name, presence: true
has_many :categorizations
has_many :categories, :through => :categorizations
accepts_nested_attributes_for :categorizations
end
class Categorization < ActiveRecord::Base
belongs_to :product
belongs_to :category
validates :description, presence: true # note the additional field here
end
class Category < ActiveRecord::Base
validates :name, presence: true
end
Run Code Online (Sandbox Code Playgroud)
我的问题始于产品新/编辑表格.
在创建产品时,我需要检查它所属的类别(通过复选框).我知道可以通过创建名称为'product [category_ids] []'的复选框来完成.但是我还需要输入每个已检查关系的描述,这些关系将存储在连接模型(Categorization)中.
我在复杂的表格,habtm复选框等上看到了那些漂亮的Railscasts.我一直在寻找StackOverflow.但我没有成功.
我发现一篇文章描述了与我几乎完全相同的问题.最后一个答案对我来说有点意义(看起来这是正确的方法).但它实际上并不是很好(即如果验证失败).我希望类别始终以相同的顺序显示(在新的/编辑表单中;在验证之前/之后)和复选框,以便在验证失败时保持原样等等.
任何人都赞赏.我是Rails的新手(从CakePHP转换)所以请耐心等待并尽可能详细地写.请以正确的方式指出我!
谢谢.:)
forms ruby-on-rails nested-forms has-many-through ruby-on-rails-3
为了表示一个组可以拥有多个用户,并且一个用户可以属于多个组,我设置了以下关联:
class Group < ActiveRecord::Base
has_many :users_groups
has_many :users, :through => :users_groups
end
class User < ActiveRecord::Base
has_many :users_groups
has_many :groups, :through => :users_groups
end
class UsersGroups < ActiveRecord::Base
belongs_to :user
belongs_to :group
end
Run Code Online (Sandbox Code Playgroud)
但是,当我键入:
Group.find(1).users
Run Code Online (Sandbox Code Playgroud)
我明白了:
NameError: uninitialized constant Group::UsersGroup
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么 ?
我有两个型号:Project和ProjectDiscipline:
class Project < ActiveRecord::Base
has_many :project_disciplinizations, :dependent => :destroy
has_many :project_disciplines, through: :project_disciplinizations
attr_accessible :project_discipline_ids
attr_accessible :project_disciplines_attributes
accepts_nested_attributes_for :project_disciplines, :reject_if => proc { |attributes| attributes['name'].blank? }
end
class ProjectDiscipline < ActiveRecord::Base
attr_accessible :name
has_many :project_disciplinizations, :dependent => :destroy
has_many :projects, through: :project_disciplinizations
end
class ProjectDisciplinization < ActiveRecord::Base
attr_accessible :project_discipline_id, :project_id
belongs_to :project_discipline
belongs_to :project
end
Run Code Online (Sandbox Code Playgroud)
在新/编辑Project表单上,我有一个学科列表和每个学科的复选框,以便用户可以选择学科:
<div class="control-group">
<label class="control-label">Check disciplines that apply</label>
<div class="controls">
<%= f.collection_check_boxes(:project_discipline_ids, ProjectDiscipline.order('name'), :id, :name, {}, {:class => …Run Code Online (Sandbox Code Playgroud) 对.这简直就是拒绝工作.在这几个小时.
专辑模型
class Album < ActiveRecord::Base
has_many :features, through: :join_table1
end
Run Code Online (Sandbox Code Playgroud)
功能模型
class Feature < ActiveRecord::Base
has_many :albums, through: :join_table1
end
Run Code Online (Sandbox Code Playgroud)
join_table1模型
class JoinTable1 < ActiveRecord::Base
belongs_to :features
belongs_to :albums
end
Run Code Online (Sandbox Code Playgroud)
join_table1架构
album_id | feature_id
Run Code Online (Sandbox Code Playgroud)
专辑架构
id | title | release_date | genre | artist_id | created_at | updated_at | price | image_path
Run Code Online (Sandbox Code Playgroud)
功能架构
id | feature | created_at | updated_at
Run Code Online (Sandbox Code Playgroud)
在调试测试数据库并运行此集成测试时:
require 'test_helper'
class DataFlowTest < ActionDispatch::IntegrationTest
test "create new user" do
album = albums(:one)
feature = features(:one)
album.features …Run Code Online (Sandbox Code Playgroud) 我有以下代码(有点简化......
create_table :signatures do |t|
t.integer :signer_id
t.integer :card_id
t.timestamps
end
Run Code Online (Sandbox Code Playgroud)
随着模型看起来像......
class Signature < ActiveRecord::Base
belongs_to :card
belongs_to :user
end
class Card < ActiveRecord::Base
has_many :signatures
has_many :signers, :through => :signatures, :foreign_key => "card_id"
end
class User < ActiveRecord::Base
has_many :sent_cards, :class_name => "Card", :foreign_key => "sender_id"
has_many :received_cards, :class_name => "Card", :foreign_key => "recipient_id"
has_many :signatures
has_many :signed_cards, :through => :signatures, :foreign_key => "signer_id"
end
Run Code Online (Sandbox Code Playgroud)
我使用rails控制台看到以下错误...
ruby-1.9.2-p0 > u15.signed_cards
ActiveRecord::HasManyThroughSourceAssociationNotFoundError: Could not find the source association(s) :signed_card or …Run Code Online (Sandbox Code Playgroud) 我有3个表 - 用户,事物和以下.用户可以通过下表跟踪事物,将a user_id与a 关联things_id.这意味着:
class User
has_many :things, :through => :follows
end
class Thing
has_many :users, :through => :follows
end
class Follow
belongs_to :users
belongs_to :things
end
Run Code Online (Sandbox Code Playgroud)
所以我可以毫无问题地检索thing.users.我的问题是如果在下面的表中,我有一个名为"relation"的列,所以我可以将一个关注者设置为"admin",我希望能够访问该关系.所以在循环中我可以做类似的事情:
<% things.users.each do |user| %>
<%= user.relation %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
有没有办法将关系包含在原始用户对象中?我试过了:select => "follows.relation",但它似乎没有加入该属性.
has-many-through ×10
associations ×5
activerecord ×3
ruby ×3
forms ×1
has-many ×1
include ×1
json ×1
many-to-many ×1
nested-forms ×1
validation ×1