虽然我不是一个完整的Ruby/Rails newb,但我仍然很绿,我正在试图弄清楚如何构建一些模型关系.我能想到的最简单的例子是烹饪"食谱"的想法.
配方由一种或多种成分和每种成分的相关数量组成.假设我们在数据库中有所有成分的主列表.这表明两个简单的模型:
class Ingredient < ActiveRecord::Base
# ingredient name,
end
class Recipe < ActiveRecord::Base
# recipe name, etc.
end
Run Code Online (Sandbox Code Playgroud)
如果我们只是想用配方原料联系起来,这是因为作为simpling添加适当的belongs_to和has_many.
但是,如果我们想将其他信息与该关系联系起来呢?每个Recipe都有一个或多个Ingredients,但我们想要指出的数量Ingredient.
什么是Rails模型的方式?这是一个什么样的线has_many through?
class Ingredient < ActiveRecord::Base
# ingredient name
belongs_to :recipe_ingredient
end
class RecipeIngredient < ActiveRecord::Base
has_one :ingredient
has_one :recipe
# quantity
end
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
end
Run Code Online (Sandbox Code Playgroud) 在我的rails应用程序中,我有两个模型:
Person和Company.我需要在这些对象的任何对之间指定多对多关系.所以我应该能够这样做:
@connections = @person.connections
Run Code Online (Sandbox Code Playgroud)
其中@connections是一个Person和Company对象的数组.
现在我已经ConnectionBinding为此创建了模型,它不能按原样运行:
class ConnectionBinding < ActiveRecord::Base
belongs_to :connect_from, polymorphic: true
belongs_to :connect_to, polymorphic: true
end
Run Code Online (Sandbox Code Playgroud)
这是个ActiveRecord::HasManyThroughAssociationPolymorphicSourceError例外
有人已经解决了这个问题吗?任何建议赞赏.
如何显示 has_many_through 关联列表,其中关联作为列标题,而 through: 值作为表行中的条目?
我有 3 个模型:
class Jobs
attr_accesor :title
has_many :scores
has_many :factors, through: :scores
end
class Scores
attr_accesor :score
belongs_to :job
belongs_to :factor
end
class Factor
attr_accesor :name
has_many :scores
has_many :jobs, through: :scores
end
Run Code Online (Sandbox Code Playgroud)
我希望能够在 Jobs 索引中显示每个 Job 的一行,每个 Factor 的标题作为列标题,以及每个 Job 的分数作为单元格中的值。
我希望必须在app/admin/jobs.rb文件中做这样的事情:
index do |jobs|
column :title
jobs.scores.each do |score|
column(score.factor.name) { |score| score.score }
end
end
Run Code Online (Sandbox Code Playgroud)
并得到这样的输出:
Job | Education | Experience | Leadership | ... |
- - - …Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails has-many-through ruby-on-rails-3 activeadmin
我有一个模型组和模型用户
它们通过"has_many:through => groups_users"连接到两个方向
groups_users表有一个名为moderator的属性,指定用户是否是该组的主持人
当我试图更新加入模块属性时,我得到一个错误
我现在这样做的方式:
@group_membership=@group.groups_users.find_by_user_id(@user.id)
if @group_membership!=nil
@group_membership.moderator=true
else
@group_membership=GroupsUser.new(:user_id => @user.id, :group_id => @group.id, :moderator => true)
end
@group_membership.save
Run Code Online (Sandbox Code Playgroud)
代码正在产生mysql错误:
Unknown column 'id' in 'where clause': UPDATE `groups_users` SET `moderator` = 1 WHERE `id` = NULL
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来做到这一点,或者我应该将id列添加到groups_users连接模型表并将其索引到id?
我有一个模型如下:
Campaign
has_many :companies, :through => :contacts
Run Code Online (Sandbox Code Playgroud)
与同一家公司有很多联系.我只想要每个公司的一个实例.
我尝试了以下方法:
@campaign = Campaign.find(params[:id])
@companies = @campaign.companies
Run Code Online (Sandbox Code Playgroud)
但这显示了我认为的每一次接触的所有公司.至少这是输出的样子.
如何确保只添加一个公司的单个实例?
我需要为Car和Store创建一个名为CarStoreTracker的连接模型,它们彼此之间有很多.
class Car < ActiveRecord::Base
has_many :carstoretrackers # It seems to work
has_many :stores, :through => :carstoretrackers # I bet the naming is not being recognized by Rails convention
end
class Store < ActiveRecord::Base
has_many :carstoretrackers # It seems to work
has_many :cars, :through => :carstoretrackers # Same issue
end
class CarStoreTracker < ActiveRecord::Base
belongs_to :store
belongs_to :car
end
Run Code Online (Sandbox Code Playgroud)
CarStoreTracker有
car_id and store_id on its table.
Run Code Online (Sandbox Code Playgroud)
当我跑:
> CarStoreTracker.first.car
> CarStoreTracker.first.store
Run Code Online (Sandbox Code Playgroud)
他们都工作.
但
Store.first.cars Car.first.stores Store.carstoretrackers Car.carstoretrackers
他们没有工作.NameError:未初始化的常量"CURRENTMODEL":: Carproducttracker
所以,我取消了CarProductTracker,我只使用了Tracker这个名称来模型,一切正常. …
ruby model ruby-on-rails naming-conventions has-many-through
我有一个has_many这样的关联:
User.rb
has_many :memberships, :dependent => :destroy
has_many :groups, :through => :memberships
Group.rb
has_many :users, :through => :memberships
has_many :microposts
Membership.rb
belongs_to :user
belongs_to :group
Run Code Online (Sandbox Code Playgroud)
首先,我需要了解获取用户所有组的查询,之后,我需要一种方法来显示这些组的所有微博。是否可以使用该信息进行一次查询?
非常感谢!
我定义了以下模型:
house_firm.rb
class HouseFirm < ActiveRecord::Base
has_many :house_firm_group_links
has_many :house_firm_groups, through: :house_firm_group_links
end
Run Code Online (Sandbox Code Playgroud)
house_firm_group.rb
class HouseFirmGroup < ActiveRecord::Base
has_many :house_firm_group_links
has_many :house_firms, through: :house_firm_group_links
end
Run Code Online (Sandbox Code Playgroud)
house_form_group_link.rb
class HouseFirmGroupLink < ActiveRecord::Base
belongs_to :house_firm
belongs_to :house_firm_group
end
Run Code Online (Sandbox Code Playgroud)
但是,当我这样做时:
@house_firm = HouseFirm.new
@house_firm.house_firm_groups
Run Code Online (Sandbox Code Playgroud)
我收到:
NameError at /house_firms/new
uninitialized constant HouseFirm::HouseFirmGroupLink
Run Code Online (Sandbox Code Playgroud)
我做错了什么,我该如何解决这个问题?