标签: has-many-through

Rails 4表单:has_many通过:复选框

原始问题

两个资源:usersanimals. 创建用户时,客户端选中复选框以说明他们有多少动物.

提交用户表单时,它不仅应创建新user记录,还应在animal_users富连接表中创建一组记录,以表示客户端选择的每个复选框.

我认为的问题是我没有为表单中的复选框部分正确指定内容.我查看了checkbox_tag API,表单上Rails指南以及许多网站和stackOverflow帖子.

在此先感谢,代码如下:

原始代码(答案代码进一步向下):

楷模:

#models/user.rb
class User < ActiveRecord::Base
  has_many :animals, through: :animal_users
  has_many :animal_users
  accepts_nested_attributes_for :animal_users, allow_destroy: true
end

#models/animal.rb
class Animal < ActiveRecord::Base
  has_many :users, through: :animal_users
  has_many :animal_users
end

#models/animal_user.rb
class AnimalUser < ActiveRecord::Base
  belongs_to :animal
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

user表格:

#views/users/_form.html.erb
    <%= form_for(@user) do |f| %>
  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through

37
推荐指数
2
解决办法
1万
查看次数

验证对象是否具有一个或多个关联对象

我需要确保在创建产品时它至少有一个类别.我可以使用自定义验证类来完成此操作,但我希望有一种更标准的方法.

class Product < ActiveRecord::Base
  has_many :product_categories
  has_many :categories, :through => :product_categories #must have at least 1
end

class Category < ActiveRecord::Base
  has_many :product_categories
  has_many :products, :through => :product_categories
end

class ProductCategory < ActiveRecord::Base
  belongs_to :product
  belongs_to :category
end
Run Code Online (Sandbox Code Playgroud)

validation ruby-on-rails has-many-through

34
推荐指数
2
解决办法
2万
查看次数

如何避免has_many:through关系中的重复?

我怎样才能实现以下目标?我有两个模型(博客和读者)和一个JOIN表,它允许我在它们之间建立N:M关系:

class Blog < ActiveRecord::Base
  has_many :blogs_readers, :dependent => :destroy
  has_many :readers, :through => :blogs_readers
end

class Reader < ActiveRecord::Base
  has_many :blogs_readers, :dependent => :destroy
  has_many :blogs, :through => :blogs_readers
end

class BlogsReaders < ActiveRecord::Base
  belongs_to :blog
  belongs_to :reader
end
Run Code Online (Sandbox Code Playgroud)

我现在想做的是将读者添加到不同的博客中.但是,条件是我只能将博客添加到博客中.因此表中不得有任何重复(相同readerID,相同blogID)BlogsReaders.我怎样才能做到这一点?

第二个问题是,如何获得读者尚未订阅的博客列表(例如,填写下拉选择列表,然后可以将读者添加到另一个博客)?

ruby ruby-on-rails duplicates has-many has-many-through

30
推荐指数
4
解决办法
2万
查看次数

Rails - 按连接表数据排序

我在工作中有一个RoR项目.以下是我的模型的适用部分.

has_many :communities, :through => :availabilities
has_many :availabilities, :order => "price ASC"
Run Code Online (Sandbox Code Playgroud)

社区

has_many :homes, :through => :availabilities
has_many :availabilities
Run Code Online (Sandbox Code Playgroud)

可用性

belongs_to :home
belongs_to :community
Run Code Online (Sandbox Code Playgroud)

数据库中的"可用性"表具有附加数据列"价格"

所以现在我可以打电话了

@home.availabilities.each do |a|
  a.community.name
  a.price
Run Code Online (Sandbox Code Playgroud)

并按我的要求取回按价格订购的可用性数据.我的问题是:

有没有办法自动订购房屋avaliabilities.first.price(第一个=最低)?也许有点什么default_scope :order

ruby join ruby-on-rails has-many-through

27
推荐指数
6
解决办法
3万
查看次数

accepted_nested_attributes_for with has_many =>:通过选项

我有两个模型,链接和标签,通过第三个link_tags关联.以下代码位于我的链接模型中.

协会:

class Link < ActiveRecord::Base
  has_many :tags, :through => :link_tags
  has_many :link_tags

  accepts_nested_attributes_for :tags, :allow_destroy => :false, 
  :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } }
end

class Tag < ActiveRecord::Base
  has_many :links, :through => :link_tags
  has_many :link_tags
end

class LinkTag < ActiveRecord::Base
  belongs_to :link
  belongs_to :tag
end
Run Code Online (Sandbox Code Playgroud)

links_controller动作:

  def new
    @link = @current_user.links.build
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @link }
    end
  end

  def create
    @link = @current_user.links.build(params[:link])

    respond_to do …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails has-many-through nested-attributes

26
推荐指数
3
解决办法
3万
查看次数

Rails RSpec测试has_many:通过关系

我是测试和rails的新手,但我正试图让我的TDD进程正常运行.

我想知道你是否使用任何类型的范例来测试has_many:通过关系?(或者我认为一般只有has_many).

例如,我发现在我的模型规范中,我肯定会编写简单的测试来检查关系方法的关系的两端.

即:

require 'spec_helper'

describe Post do

  before(:each) do
    @attr = { :subject => "f00 Post Subject", :content => "8ar Post Body Content" }
  end

  describe "validations" do
  ...    
  end

  describe "categorized posts" do

    before(:each) do
      @post  = Post.create!(@attr)
    end

    it "should have a categories method" do
      @post.should respond_to(:categories)
    end

  end

end
Run Code Online (Sandbox Code Playgroud)

然后在我的类别规范中,我做反向测试并检查@ category.posts

我还缺少什么?谢谢!!

ruby rspec ruby-on-rails has-many has-many-through

23
推荐指数
2
解决办法
2万
查看次数

如何通过关联在has_many中使用回调?

我有一个通过has_many通过项目模型关联的任务模型,需要在通过关联删除/插入之前操作数据.

由于" 自动删除连接模型是直接的,因此不会触发销毁回调. "我无法使用回调.

在任务中,我需要所有project_ids在保存任务后计算Project的值.如何通过关联禁用删除或更改删除以销毁has_many?这个问题的最佳做法是什么?

class Task
  has_many :project_tasks
  has_many :projects, :through => :project_tasks

class ProjectTask
  belongs_to :project
  belongs_to :task

class Project
  has_many :project_tasks
  has_many :tasks, :through => :project_tasks
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails callback has-many-through ruby-on-rails-3

23
推荐指数
1
解决办法
1万
查看次数

rails has_many:through - 是否可以在直通表中有条件?

有两个模型,它们使用has_many:尽管关系链接.

有条件参数,它将在另一个模型表中查找条件,但是在某种程度上是否在连接表中创建条件?

例如,我有:

User
Game
GameUser
Run Code Online (Sandbox Code Playgroud)

一个用户可能有很多游戏,因为游戏可能有很多用户.但我想在联合表中存储额外的信息,例如,如果用户喜欢或不喜欢该游戏.

我希望在我的用户模型中有一个关系过滤器,如下所示:

has_many :games, :through => 'game_users'   
has_many :liked_games, :through => 'game_users', :conditions_join => { :like => true }
Run Code Online (Sandbox Code Playgroud)

有没有一种很好的方法来实现这个功能?

ruby-on-rails has-many-through ruby-on-rails-3

22
推荐指数
2
解决办法
2万
查看次数

Rails has_many:通过嵌套表单

我刚刚加入has_many :through联盟.我试图实现保存所有3个表数据(的能力Physician,Patient通过一个单一的形式和关联表).

我的迁移:

class CreatePhysicians < ActiveRecord::Migration
  def self.up
    create_table :physicians do |t|
      t.string :name
      t.timestamps
    end
  end
end

class CreatePatients < ActiveRecord::Migration
  def self.up
    create_table :patients do |t|
      t.string :name
      t.timestamps
    end
  end
end

class CreateAppointments < ActiveRecord::Migration
  def self.up
    create_table :appointments do |t|
      t.integer :physician_id
      t.integer :patient_id
      t.date :appointment_date
      t.timestamps
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的模特:

class Patient < ActiveRecord::Base
  has_many :appointments
  has_many :physicians, :through => :appointments
  accepts_nested_attributes_for :appointments
  accepts_nested_attributes_for :physicians
end
class Physician …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-forms has-many-through

22
推荐指数
3
解决办法
2万
查看次数

has_many:通过外键?

我已经阅读了多个有关此问题的问题,但尚未找到适合我情况的答案.

我有3种型号:Apps,AppsGenresGenres

以下是每个相关领域:

Apps
application_id

AppsGenres
genre_id
application_id

Genres
genre_id
Run Code Online (Sandbox Code Playgroud)

这里的关键是我没有使用id这些模型中的字段.

我需要根据这些application_idgenre_id字段关联表.

这是我目前得到的,但它没有得到我需要的查询:

class Genre < ActiveRecord::Base
  has_many :apps_genres, :primary_key => :application_id, :foreign_key => :application_id
  has_many :apps, :through => :apps_genres
end

class AppsGenre < ActiveRecord::Base
  belongs_to :app, :foreign_key => :application_id
  belongs_to :genre, :foreign_key => :application_id, :primary_key => :application_id
end

class App < ActiveRecord::Base
  has_many :apps_genres, :foreign_key => :application_id, :primary_key => :application_id
  has_many :genres, :through => :apps_genres
end …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails foreign-keys associations has-many-through

22
推荐指数
1
解决办法
2万
查看次数