class Agents << ActiveRecord::Base
belongs_to :customer
belongs_to :house
end
class Customer << ActiveRecord::Base
has_many :agents
has_many :houses, through: :agents
end
class House << ActiveRecord::Base
has_many :agents
has_many :customers, through: :agents
end
Run Code Online (Sandbox Code Playgroud)
如何添加Agents
模型Customer
?
这是最好的方法吗?
Customer.find(1).agents.create(customer_id: 1, house_id: 1)
Run Code Online (Sandbox Code Playgroud)
以上工作正常从控制台,但我不知道如何在实际应用程序中实现这一点.
想象一下,为客户填写的表格也house_id
作为输入.然后在我的控制器中执行以下操作?
def create
@customer = Customer.new(params[:customer])
@customer.agents.create(customer_id: @customer.id, house_id: params[:house_id])
@customer.save
end
Run Code Online (Sandbox Code Playgroud)
总的来说,我对如何在has_many :through
表中添加记录感到困惑?
有人能告诉我,如果我只是以错误的方式进行设置吗?
我有以下具有has_many.through关联的模型:
class Listing < ActiveRecord::Base
attr_accessible ...
has_many :listing_features
has_many :features, :through => :listing_features
validates_presence_of ...
...
end
class Feature < ActiveRecord::Base
attr_accessible ...
validates_presence_of ...
validates_uniqueness_of ...
has_many :listing_features
has_many :listings, :through => :listing_features
end
class ListingFeature < ActiveRecord::Base
attr_accessible :feature_id, :listing_id
belongs_to :feature
belongs_to :listing
end
Run Code Online (Sandbox Code Playgroud)
我正在使用Rails 3.1.rc4,FactoryGirl 2.0.2,factory_girl_rails 1.1.0和rspec.这是我对:listing
工厂的基本rspec rspec健全性检查:
it "creates a valid listing from factory" do
Factory(:listing).should be_valid
end
Run Code Online (Sandbox Code Playgroud)
这是工厂(:上市)
FactoryGirl.define do
factory :listing do
headline 'headline'
home_desc 'this is the home …
Run Code Online (Sandbox Code Playgroud) Ruby和Rails都是新手,但我现在已经预订了教育(显然没什么意义,哈哈).
我有两个模型,Event和User通过表EventUser加入
class User < ActiveRecord::Base
has_many :event_users
has_many :events, :through => :event_users
end
class EventUser < ActiveRecord::Base
belongs_to :event
belongs_to :user
#For clarity's sake, EventUser also has a boolean column "active", among others
end
class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
end
Run Code Online (Sandbox Code Playgroud)
这个项目是一个日历,我必须跟踪人们注册并为特定事件划出他们的名字.我认为多对多是一个很好的方法,但我做不到这样的事情:
u = User.find :first
active_events = u.events.find_by_active(true)
Run Code Online (Sandbox Code Playgroud)
因为事件实际上没有那些额外的数据,所以EventUser模型可以.虽然我能做到:
u = User.find :first
active_events = []
u.event_users.find_by_active(true).do |eu|
active_events << eu.event
end
Run Code Online (Sandbox Code Playgroud)
这似乎与"铁路方式"相反.任何人都可以启发我,今晚(今天早上)这已经困扰了我很长时间?
鉴于以下AR模型,我希望在给定任务句柄时按姓氏按字母顺序对用户进行排序:
#user
has_many :assignments
has_many :tasks, :through => :assignments
#assignment
belongs_to :task
belongs_to :user
#task
has_many :assignments
has_many :users, :through => :assignments
Run Code Online (Sandbox Code Playgroud)
我想获得一个任务然后导航到其分配的用户,并按字母顺序对用户列表进行排序.
我一直在想我应该能够添加这样的:order
子句has_many :users, :through => :assignments
:
#task.rb
has_many :assignments
has_many :users, :through => :assignments, :order => 'last_name, first_name'
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
如何last_name
在给定任务时对用户进行排序?
是否有可能has_many :through
在Rails中有多个相互关系的关系?我收到了这样的建议,作为我发布的另一个问题的解决方案,但一直无法让它工作.
好友是通过联接表的循环关联.我的目标是创建一个has_many :through
for friends_comments
,所以我可以采取行动,User
并user.friends_comments
在一个查询中获取他的朋友发表的所有评论.
class User
has_many :friendships
has_many :friends,
:through => :friendships,
:conditions => "status = #{Friendship::FULL}"
has_many :comments
has_many :friends_comments, :through => :friends, :source => :comments
end
class Friendship < ActiveRecord::Base
belongs_to :user
belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"
end
Run Code Online (Sandbox Code Playgroud)
这看起来很棒,而且很有意义,但不适合我.当我尝试访问用户的friends_comments时,这是我在相关部分中遇到的错误:
ERROR: column users.user_id does not exist
: SELECT "comments".* FROM "comments" INNER JOIN "users" ON "comments".user_id = "users".id WHERE (("users".user_id = …
当使用:through选项时,显然依赖=> destroy被忽略.
所以我有这个......
class Comment < ActiveRecord::Base
has_many :comment_users, :dependent => :destroy
has_many :users, :through => :comment_users
...
end
Run Code Online (Sandbox Code Playgroud)
...但删除评论不会导致关联的comment_user记录被删除.
那么,当使用时,对于级联删除,推荐的方法是什么:通过?
谢谢
我想做什么:
我有一个博客,想在主帖后面显示相关帖子.
class Post < ActiveRecord::Base
has_many :related_posts
has_many :posts, :through => :related_posts
end
Run Code Online (Sandbox Code Playgroud)
然后在连接模型/表中
class RelatedPost < ActiveRecord::Base
belongs_to :post
end
Run Code Online (Sandbox Code Playgroud)
当然还有一个叫related_posts
两post_id
列的表.
显然这有几个缺点,我只是不确定如何在Rails中使这个关联工作.
我有一个has_many through
关联工作的问题.
我一直得到这个例外:
Article.find(1).warehouses.build
ActiveRecord::HasManyThroughAssociationNotFoundError: Could not find the association :entries in model Article
Run Code Online (Sandbox Code Playgroud)
这些是涉及的模型:
class Article < ActiveRecord::Base
has_many :warehouses, :through => :entries
end
class Warehouse < ActiveRecord::Base
has_many :articles, :through => :entries
end
class Entry < ActiveRecord::Base
belongs_to :article
belongs_to :warehouse
end
Run Code Online (Sandbox Code Playgroud)
这是我的架构:
create_table "articles", :force => true do |t|
t.string "article_nr"
t.string "name"
t.integer "amount"
t.string "warehouse_nr"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "unit"
end
create_table "entries", :force => true do |t|
t.integer "warehouse_id"
t.integer "article_id" …
Run Code Online (Sandbox Code Playgroud) 这对我来说可能是一个非常基本的疏忽,但我似乎无法想起一个简单的方法来删除两个通过连接的对象之间的关联has_many :through
.IE:
class Photo
has_many :tags, :through => :taggings
has_many :taggings, :dependent => :destroy
end
class Tags
has_many :photos, :through => :taggings
has_many :taggings, :dependent => :destroy
end
class Taggings
belongs_to :photo
belongs_to :tag
end
Run Code Online (Sandbox Code Playgroud)
如果你有两个对象,tag
并且photo
你可以通过这样做来关联它们:
photo.tags << tag
Run Code Online (Sandbox Code Playgroud)
那么,这有一个同样简单的对立面吗?即:
photo.tags.remove tag
Run Code Online (Sandbox Code Playgroud) A Person
可以有很多Events
,每个都Event
可以有一个多态Eventable
记录.如何指定记录Person
与Eventable
记录之间的关系?
以下是我的模型:
class Event < ActiveRecord::Base
belongs_to :person
belongs_to :eventable, :polymorphic => true
end
class Meal < ActiveRecord::Base
has_one :event, :as => eventable
end
class Workout < ActiveRecord::Base
has_one :event, :as => eventable
end
Run Code Online (Sandbox Code Playgroud)
主要问题涉及到Person
班级:
class Person < ActiveRecord::Base
has_many :events
has_many :eventables, :through => :events # is this correct???
end
Run Code Online (Sandbox Code Playgroud)
我是否has_many :eventables, :through => :events
像上面那样说?
或者我必须像这样拼写它们:
has_many :meals, :through => :events
has_many :workouts, …
Run Code Online (Sandbox Code Playgroud) activerecord ruby-on-rails has-many-through polymorphic-associations
has-many-through ×10
activerecord ×5
associations ×2
ruby ×2
factory-bot ×1
has-many ×1
rspec ×1