Tag: has and belongs to many

Rails habtm并找到没有关联的记录

我有2个型号: class User < ActiveRecord::Base has_and_belongs_to_many :groups end class Group < ActiveRecord::Base has_and_belongs_to_many :users end 我想创建一个范围 (这对于效率和链接范围的能力很重要),它返回不属于任何组的用户。 经过多次尝试,我没有做一个方法而不是范围,这使得在User.all上collect丑陋而且……不对。 有帮助吗? 也许对于第二个问题:我设法创建一个范围,返回属于任何给定组的用户(以id的数组给出)。 scope :in_groups, lambda { |g| { :joins => :groups, :conditions => {:groups => {:id => g}}, :select => “DISTINCT `users`.*” # kill duplicates } } 可以更好/更漂亮吗? (使用Rails 3.0.9)

Rails:拥有并且属于许多(HABTM) – 创建关联而不创建其他记录

在Google上花了一整天,但找不到答案。 :\ 我在用户和Core_Values之间有HABTM关系。 class CoreValue < ActiveRecord::Base has_and_belongs_to_many :users class User < ActiveRecord::Base has_and_belongs_to_many :core_values 在我的控制器中,我需要做两件事: 如果CoreValue不存在,请创建一个新的并将其与给定的用户ID相关联,并且 假设我知道特定的CoreValue确实存在,则创建关联而不创建任何新的CoreValues或Users 对于#1,我有这个工作: User.find(current_user.id).core_values.create({:value => v, :created_by => current_user.id}) 这将创建一个新的CoreValue:value和:created_by并创建关联。 对于#2,我尝试了一些东西,但似乎不太可能只创建关联。 谢谢你的帮助!

Rails 3 has_and_belongs_to_many关联:如何分配相关对象而不将它们保存到数据库

使用has_and_belongs_to_many_association class Category has_and_belongs_to_many :projects end 我想在保存类别之前使用before_filter来设置项目 before_filter :set_projects, :only => [:create, :update] def set_projects @category.assign_attributes({projects: Project.all}) end 这种方法很有效,除非无法保存类别并且存在回滚。 项目仍在数据库中更新。 为什么这一行 @category.assign_attributes({projects: Project.all}) 立即生成这些数据库记录? BEGIN INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 1) INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 2) INSERT INTO “categories_projects” (“category_id”, “project_id”) VALUES (86, 3) COMMIT 在提交这些新的categories_projects关系之前,我想等待@ category.save。 如何推迟这些提交? 请注意,我无法修改主“更新”操作。 我必须在filter和回调之前/之后使用,以覆盖我的应用程序的当前function。 […]

需要帮助来理解`has_and_belongs_to_many`

数据库中有三个表: 用户(非空) 时间表(非空) schedules_users( 空 ) 用户模型: class User < ActiveRecord::Base […] has_and_belongs_to_many :schedules#has_many :schedules […] end 时间表模型: class Schedule < ActiveRecord::Base has_and_belongs_to_many :users#belongs_to :user end welcome-controller 🙁我希望按日期和时间对日程安排进行排序) class WelcomeController < ApplicationController def index if(current_user) @user_schedules = current_user.schedules @user_schedules_date = @user_schedules.order(:date_time).group_by { |sched| sched.date_time.beginning_of_day } end end end @user_schedules = current_user.schedules这不正确,不是吗? SQL输出: Started GET “/” […]

Rails:通过form_for check_box为HABTM关系分配多个参数

我是第一个自己项目的铁杆菜鸟。 我使用has_and_belongs_to_many关系链接了两个模型:Wine和Shop。 为了简单起见,葡萄酒可以在不同的商店出售,特定的商店可以出售许多不同的葡萄酒。 以下是模型: class Shop < ActiveRecord::Base has_and_belongs_to_many :wines end class Wine < ActiveRecord::Base has_and_belongs_to_many :shops end 我的目标是创建一个表单来创建Wine的实例,包括可以购买葡萄酒的商店。 这是我的wines_controller: def new @wine = wine.new @shops = Shop.all respond_to do |format| format.html # new.html.erb format.json { render json: @wine } end end def create @wine = Wine.new(params[:wine]) params[:shops].each do |id| @wine.shops << Shop.find(id) end end […]

Rails 4多对多关联不起作用

Ruby on rails新手在这里。 试图创建一个入门博客应用程序,并在我的模型之间的多对多关联中遇到问题。 我有2个模型 – post,类别,彼此之间有多对多关联。 我的问题:当我创建一个新post时,Post会被保存,但是类别后关联不会保存在categories_posts表中。 我的代码如下。 感谢您对此的投入。 post.rb class Post < ActiveRecord::Base validates_presence_of :title, :body, :publish_date belongs_to :user has_and_belongs_to_many :categories end category.rb class Category < ActiveRecord::Base validates_presence_of :name has_and_belongs_to_many :posts end categories_posts.rb class CategoriesPosts < ActiveRecord::Base end 迁移 – create_posts.rb class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title […]

DataMapper有n通过资源DELETE(从关联中删除)不起作用

我有这两节课, class User include DataMapper::Resource property :id, Serial property :name, String has n :posts, :through => Resource end class Post include DataMapper::Resource property :id, Serial property :title, String property :body, Text has n :users, :through => Resource end 所以,一旦我有一个新post,如: Post.new(:title => “Hello World”, :body = “Hi there”).save 我在添加和删除关联时遇到严重问题,例如: User.first.posts << Post.first #why do I have […]