Tag: 模型关联

nested_form没有保存到模型Rails 3

虽然我无法将表单数据保存到模型中,但我认为我正处于以下方面。 我有2个型号 class Prediction < ActiveRecord::Base attr_accessible :home_team, :away_team, :home_score, :away_score, :fixtures_attributes has_many :fixtures accepts_nested_attributes_for :fixtures end class Fixture < ActiveRecord::Base attr_accessible :home_team, :away_team, :fixture_date, :kickoff_time belongs_to :predictions end 为了创建一个新的预测记录,我有一个表单可以获取所有的灯具并预先填充表单,用户只需在每个团队旁边添加分数 <%= f.fields_for :fixtures, @fixtures do |ff| %> VS 然后我让我的控制器来处理新的/创建动作,我认为这是我可能会摔倒的地方 class PredictionsController ‘Predictions Submitted Successfully’ else render ‘new’ end end end 最后我的路线 resources :predictions resources :fixtures […]

如何实现具有3个固定分类的jobpostfunction

我想要做的是创建放置模型,用于发布3个固定类别的工作,即实习,公司和自由职业。将有一个安置菜单,将有3个menues作为实习,公司和自由职业者,并根据用户选择,对于这3个类别,将显示相同的职位发布表格以创建职位。 在显示工作岗位时,将根据类别显示不同的视图。 我没有得到我应该如何实现这一点。我是否应该创建位置和类别作为一个不同的模型,并给予关联作为多个类别的位置和属于类别的位置,但如果我这样做,在类别模型我有3固定的选择,我不会接受用户的选择,那么我如何在模型中添加这3个选项将被修复并按类别添加它们的工作岗位? 我该如何实现这个展示位置?

Ruby on Rails – 创建新条目时,使用回调将两个对象在单独的表中相乘

我有3个表,目标,活动和Goal_Activities。 目标有一个xp的设定值,而Activity接受来自用户的输入。 使用Activity中的on_create方法,我想将用户输入与目标xp相乘并将其存储在目标活动表中,这样我就可以保留每个更新的记录,并最终将它们全部添加到一起以创建总目标xp in目标表。 我在SO, 这里和这里找到了几个关于类似主题的问题,但我发现很难理解模型文件之间的关联以及如何访问不同模型中的某些方法。 我还阅读了关于回调的文档,我认为这是我需要设置的,但如果有更好的方法,请告诉我。 这是我目前的模特协会。 goal.rb class Goal < ActiveRecord::Base has_many :goal_activities has_many :activities, through: :goal_activities end goal_activity.rb class GoalActivity < ActiveRecord::Base belongs_to :goal belongs_to :activity end activity.rb class Activity < ActiveRecord::Base has_many :goal_activities belongs_to :goal, through: :goal_activities end 以下是表中的一组示例值。 目标 goal_id:1,xp:500,total_goal_xp:默认值为0 活动 activity_id:1,数量:3,goal_id:1 Goal_activities goal_activity_id:1,goal_id:1,activity_id:1,total_xp:1500 一旦创建了活动,total_xp就会被添加到目标表中的total_goal_xp中。 任何意见,将不胜感激。 如果您有任何疑问,请告诉我。

在子模型中获取父值

我有一个名为RsvpRegistrations的模型 belongs_to :rsvp 我需要在我的validation中使用来自父’rsvp’对象的值,例如 validates_presence_of :phone if self.rsvp.phone (Rsvp.phone是布尔值) 但这不起作用。 我得到的错误是未定义的方法`rsvp’。 如何访问父对象及其值? 一旦我运行它,我有其他类似的validation运行,所以我想我需要抓住父’rsvp’一次,然后在我的其他validation中引用它。 提前致谢。

数据建模3路表has_many关联

我正在尝试构建一个表来处理某个广告系列已使用以下模型关联设置的位置和类别: class Campaign < ActiveRecord::Base has_many :campaign_category_metro_bids, dependent: :destroy has_many :metros, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Metro < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :categories, through: :campaign_category_metro_bids end class Category < ActiveRecord::Base has_many :campaign_category_metro_bids has_many :campaigns, through: :campaign_category_metro_bids has_many :metros, through: :campaign_category_metro_bids end class CampaignCategoryMetroBid < ActiveRecord::Base belongs_to :campaign belongs_to […]

has_many的源reflection错误:通过

我正在尝试创建一个系统,我的网站的用户可以在其中collections页面。 这些页面有两种类型,俱乐部或体育。 所以,我有四个模型,相关联: 用户模型: class User :favorites has_many :clubs, :through => :favorites .. end collections夹型号: class Favorite true end 俱乐部型号: class Club :favoritable has_many :users, :through => :favorites def to_param slug end end 运动模型: class Sport :favoritable has_many :users, :through => :favorites .. end 从本质上讲,用户可以通过collections夹进行体育或俱乐部,而collections,体育和俱乐部之间的关联是多态的。 在实践中,这一切都按照我想要的方式工作,并且我设计的整个系统都有效。 但是,我在我的网站上使用Rails_Admin,我在三个地方收到错误: 第一次加载仪表板(/ admin)时。 如果我刷新页面,它工作正常。 在Rails_Admin中加载用户模型时 在Rails_Admin中加载collections夹模型时 这是/admin/user (gist)上的错误消息。 所有错误都是类似的,引用ActiveRecord::Reflection::ThroughReflection#foreign_key […]

Rails 4:validationid或关联的存在之间的区别

如果我在模型中有’belongs_to’关联,我想知道validation关联之间的名义差异: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake, presence: true … 并validation关联的模型ID: class Topping < ActiveRecord::Base belongs_to :pancake validates :pancake_id, presence: true … 动机: 为煎饼分配顶部的一些代码在过去的某个时间停止了工作。 将validation从关联更改为id’修复’问题,但我想知道更深层次的原因。 (仅供参考,当进入代码时,煎饼有效且在数据库中,顶部适当地响应.pancake和.pancake_id 。推送操作符( pancake.toppings << topping )和手动分配并保存( topping.pancake = pancake; topping.save )因煎饼缺少validation错误而失败。)

在rails中实现友谊模型的最佳方式

我想在我的应用程序中实现用户的朋友系统,所以我发现rails空间解决方案非常好,想法是在the Friendships table创建两行 :发送者邀请的第一行,接收者的第二行 用户之间的关系设置为has_many关联,如下所示: has_many :friendships has_many :friends, :through => :friendships, :conditions => “status = ‘accepted'” 接受用户为朋友的方法如下: # Accept a friend request. def self.accept(user, friend) transaction do accepted_at = Time.now accept_one_side(user, friend, accepted_at) accept_one_side(friend, user, accepted_at) end end accept_one_side()方法是: # Update the db with one side of an accepted friendship request. def self.accept_one_side(user, friend, […]

validation失败类必须存在

我在Rails中遇到了很多关联的问题。 我发现了很多类似的问题,但我不能申请我的案子: 城市class级: class City < ApplicationRecord has_many :users end 用户class级: class User < ApplicationRecord belongs_to :city validates :name, presence: true, length: { maximum: 80 } validates :city_id, presence: true end 用户控制器: def create Rails.logger.debug user_params.inspect @user = User.new(user_params) if @user.save! flash[:success] = “Works!” redirect_to ‘/index’ else render ‘new’ end end def user_params params.require(:user).permit(:name, :citys_id) […]

Rails4动态选择下拉列表

我正在尝试使用form_tag在搜索表单中设置一些动态下拉选择菜单。 我想要的是与Railcasts#88中的示例类似的function 楷模: class Count < ActiveRecord::Base belongs_to :host end class Host < ActiveRecord::Base belongs_to :site has_many :counts end class Site < ActiveRecord::Base belongs_to :state has_many :hosts end class State < ActiveRecord::Base has_many :sites end 视图: “get”, id: “search-form”) do %> 一个州拥有多个拥有许多计数的主机的网站。 或者相反,Counts belongs_to Host thats属于哪个属于State的Site 所以我想从州下拉列表中选择一个状态,然后根据他们通过主机关联的状态“分组”站点。 我一直在努力与这个嵌套关联,似乎无法弄清楚如何构建groups_collection_select。 我知道我忽略了一些明显的东西! 可以肯定使用一些指针……