标签: has-many-through

模型 - > has_many - >两次

所以我在这里有一个有点混乱的关系,在Note,Group和User之间.我最终在模型中使用了has_many两次.但我目前专注于Note&Group关系.

背景:一个小组可以有一个注释.用户也可以有笔记.这就是我的Note是多态的原因.但是,我还创建了一个名为Tag的连接模型,因此Note可以属于多个组.在我的代码中,我最终得到了多个'has_many:notes'.请参阅下面的所有代码.做这样的事情的正确方法是什么?

提前致谢!

note.rb

belongs_to :notable, :polymorphic => true
has_many :tags
has_many :groups, :through => :tags
Run Code Online (Sandbox Code Playgroud)

user.rb

has_many :notes, :as => :notable
Run Code Online (Sandbox Code Playgroud)

group.rb

has_many :notes, :as => :notable
has_many :tags
has_many :notes, :through => :tags
Run Code Online (Sandbox Code Playgroud)

tag.rb

belongs_to :note
belongs_to :group
Run Code Online (Sandbox Code Playgroud)

model ruby-on-rails associations has-many has-many-through

4
推荐指数
1
解决办法
941
查看次数

我是否需要has_many的联接表:通过关联?

我之前只创建了一个has_and_belongs_to_many关联,它与has_many:through不同.对于has_many:通过关联,我需要一个连接表吗?实际协会如何运作?我需要索引吗?我找不到一个很棒的教程,有什么建议吗?

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

4
推荐指数
1
解决办法
8511
查看次数

Rails复杂视图表单与has_many:通过关联

我正在尝试为食谱创建一个rails应用程序,但我对如何创建视图表单和控制器逻辑感到困惑.我有2个模型,配方和项目,加入has_many :through与Ingredient模型的关联如下:

class Recipe < ActiveRecord::Base
    has_many :ingredients
    has_many :items, :through => :ingredients
end

class Item < ActiveRecord::Base
    has_many :ingredients
    has_many :recipes, :through => :ingredients
end

class Ingredient < ActiveRecord::Base
    # Has extra attribute :quantity
    belongs_to :recipe
    belongs_to :item
end
Run Code Online (Sandbox Code Playgroud)

此关联在控制台中有效.例如:

Recipe.create( :name => 'Quick Salmon' )
Item.create( :name => 'salmon', :unit => 'cups' )
Ingredient.create( :recipe_id => 1, :item_id => 1, :quantity => 3)

Recipe.first.ingredients
=> [#<Ingredient id: 1, recipe_id: 1, item_id: 1, quantity: 3]

Recipe.first.items
=> …
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
3860
查看次数

Rails has_many:通过关联.删除与链接的关联?

我有一个事件模型和一个通过Attendees模型加入的用户模型.我已经想出如何作为经过身份验证的用户"参加"活动.但我无法弄清楚的是从事件"退出"的好方法.我确信这是一件微不足道的事情,我错过了但是有什么更好的方式来进入StackOverflow而不是问一些微不足道的事情?哦,我一直在搜索railscasts和SO几个小时......

谢谢!

观点/事件/ show.html.erb

<p><strong>Attendees: </strong>
    <ul>
        <% for attendee in @event.users %>
            <% if attendee.username == current_user.username %>
                <li><strong><%= attendee.username %></strong>
                    <%= link_to 'Withdraw From Event', withdraw_event_path(@event.id), :method => :post, :class => 'btn btn-danger' %>
                    <%= link_to 'Destroy', @attendee, confirm: 'Are you sure?', method: :delete, :class => 'btn btn-danger' %>
                </li>
            <% else %>
                <li><%= attendee.username %></li>
            <% end %>
        <% end %>   
    </ul>
</p>
Run Code Online (Sandbox Code Playgroud)

/controllers/events_controller.rb

  def attend
    @event = Event.find(params[:id])
    current_user.events << @event
    redirect_to @event, notice: 'You have …
Run Code Online (Sandbox Code Playgroud)

ruby activerecord ruby-on-rails has-many-through

4
推荐指数
1
解决办法
3081
查看次数

Rails has_many通过条件,构建新的

我有用户和组织加入模型UsersOrganisation.用户可能是组织的管理员 - 如果是,则is_admin布尔值为true.

如果我在数据库中手动设置is_admin布尔值,Organisations.admins就像我期望的那样工作.

在控制台中,我可以这样做Organisation.first.users << User.first,它会像我期望的那样创建一个organisations_users条目.

但是,如果我这样做Organisation.first.admins << User.last会创建一个普通用户,而不是管理员,即连接表上的is_admin布尔值未正确设置.

除了直接在连接表中创建条目之外,有没有其他方法可以做到这一点?

class User < ActiveRecord::Base

  has_many :organisations_users
  has_many :organisations, :through => :organisations_users

end

class Organisation <  ActiveRecord::Base

  has_many :organisations_users
  has_many :users, :through => :organisations_users
  has_many :admins, :through => :organisations_users, :class_name => "User", 
            :source => :user, 
            :conditions => {:organisations_users => {:is_admin => true}}

end

class OrganisationsUser < ActiveRecord::Base

  belongs_to :organisation
  belongs_to :user

end
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
1397
查看次数

rails 3 has_many通过has_one

假设您有以下型号:

class Category < ActiveRecord::Base
  has_one :current_heat, class_name: 'Heat'
  has_many :scores, :through => :current_heat
end

class Heat < ActiveRecord::Base
  belongs_to :category
  has_many :scores
end

class Score < ActiveRecord::Base  
  belongs_to :heat
end
Run Code Online (Sandbox Code Playgroud)

令人惊讶的是,当我调用Category.first.scoresActiveRecord时会产生以下查询:

SELECT `categories`.* FROM `categories` LIMIT 1
SELECT * FROM `scores` INNER JOIN `heats` ON `scores`.`heat_id` = `heats`.`id` WHERE `heats`.`category_id` = 1
Run Code Online (Sandbox Code Playgroud)

上面的查询忽略了has_one的性质Category#current_heat.我原本期待的更像是:

SELECT `categories`.* FROM `categories` LIMIT 1
SELECT `heats`.* FROM `heats` WHERE `heats`.`category_id` = 1 LIMIT 1
SELECT * FROM `scores` WHERE `scores`.`heat_id` …
Run Code Online (Sandbox Code Playgroud)

activerecord ruby-on-rails has-many-through

4
推荐指数
1
解决办法
2512
查看次数

Rails 3,多对多形式使用accepts_nested_attributes_for,如何正确设置?

我在食谱配料之间有多对多的关系.我正在尝试构建一个允许我在配方中添加成分的表单.

(这个问题的变体已被反复询问,我花了好几个小时,但基本上对于accepts_nested_attributes_for它的含义感到困惑.)

在您对以下所有代码感到害怕之前,我希望您会发现它确实是一个基本问题.这是非恐怖的细节......

错误

当我显示一个表单来创建一个食谱时,我收到错误"未初始化的常量Recipe :: IngredientsRecipe",指向我的表单中的一行

18:   <%= f.fields_for :ingredients do |i| %>
Run Code Online (Sandbox Code Playgroud)

如果我改变这一行,使"成分"单数

<%= f.fields_for :ingredient do |i| %>
Run Code Online (Sandbox Code Playgroud)

然后表单显示,但是当我保存时,我得到一个质量分配错误Can't mass-assign protected attributes: ingredient.

模型(3个文件,相应命名)

class Recipe < ActiveRecord::Base
  attr_accessible :name, :ingredient_id
  has_many :ingredients, :through => :ingredients_recipes
  has_many :ingredients_recipes

  accepts_nested_attributes_for :ingredients
  accepts_nested_attributes_for :ingredients_recipes
end

class Ingredient < ActiveRecord::Base
  attr_accessible :name, :recipe_id
  has_many :ingredients_recipes
  has_many :recipes, :through => :ingredients_recipes

  accepts_nested_attributes_for :recipes
  accepts_nested_attributes_for :ingredients_recipes
end

class IngredientsRecipes < ActiveRecord::Base
  belongs_to …
Run Code Online (Sandbox Code Playgroud)

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

4
推荐指数
1
解决办法
3711
查看次数

Rails:在多个嵌套的restful路由中嵌套相同的资源?

这很简单,但是在框架/语言之间切换回来让我自己怀疑.试图has_many :through在Rails中找出关系路由.

#User
has_many :reservations, :dependent => :destroy
has_many :events, :through => :reservations

#Reservation
belongs_to :user
belongs_to :event

#Event
has_many :reservations, :dependent => :destroy
has_many :attendees, through => :reservations, :source => :user
Run Code Online (Sandbox Code Playgroud)

我想要路线,从用户模型我可以快速建立/创建一个新的预订到特定的事件(反之亦然),但不知道如何创建它们.这看起来很奇怪:

resources :users do
  resources :reservations, only: [:new, :create, :destroy]
end

resources :events do
  resources :reservations, only: [:new, :create, :destroy]
end
Run Code Online (Sandbox Code Playgroud)

无论如何,只是寻找验证,或者可能是另一种方法来处理这个问题.理想情况下,在用户可以点击的事件旁边的页面上有一个链接,(通过ajax或页面重新加载)他们会进行"预订".

出于同样的原因,我需要路由来查看对给定事件有预订的每个用户.并且保留给定用户的每个事件.

routes ruby-on-rails has-many-through

4
推荐指数
1
解决办法
914
查看次数

如何使用simple_form和嵌套表单通过关联为has_many创建嵌套表单?

我有一个rails应用程序,有一个专辑和歌曲模型,有很多关系.我正在尝试使用simple_formnested_form gems 将歌曲添加到相册.

如果我使用simple_form,则很容易创建关联,但是我无法使用nested_form.看来这应该有效:

<%= f.fields_for :songs do |song_form| %>
  <%= song_form.association :songs %>
  <%= song_form.link_to_remove "Remove this song" %>
<% end %>
<p><%= f.link_to_add "Add a song", :songs %></p>
Run Code Online (Sandbox Code Playgroud)

但是我得到了这个错误:RuntimeError in Albums#new Association :songs not found.如果我只使用simple_form,该关联工作正常.

正确的语法是什么?或者这些宝石不兼容?如果两个宝石不兼容,那么如何使用nested_form添加和删除相册中的歌曲?

/ views/albums/_form https://gist.github.com/leemcalilly/51e7c5c7e6c4788ad000

/ models/album https://gist.github.com/leemcalilly/9a16f43106c788ab6877

/ models/song https://gist.github.com/leemcalilly/0ccd29f234f6722311a0

/ models/albumization https://gist.github.com/leemcalilly/c627ad2b178e1e11d637

/ controllers/albums_controller https://gist.github.com/leemcalilly/04edf397b2fb2a3d0d1d

/ controllers/songs_controller https://gist.github.com/leemcalilly/bcbccc9259c39d0b6b7a

ruby-on-rails nested-forms has-many-through nested-form-for simple-form

4
推荐指数
1
解决办法
2637
查看次数

Active Admin has_many可选择的记录列表

我已经尝试了好几天了,我是ROR和活跃管理员的新手.到目前为止,我已经能够为新记录添加和删除has_many关系.我使用的是strong_parameters以及accept_nested_attributes.我想要

  • 能够添加和删除现有记录的关系.

理想情况下,应该有一个自动完成框,允许搜索和选择此特定模型的现有含义.

我的模特是

  • 含义
  • WordMeaning

我只想要附加已经可用于单词的含义的功能吗?

      class Word < ActiveRecord::Base    
         belongs_to :language    
         has_many :word_meanings
         has_many :meanings ,through: :word_meanings
Run Code Online (Sandbox Code Playgroud)
form do |f|

f.semantic_errors *f.object.errors.keys
f.inputs do
  f.input :language
  f.input :word
  f.input :wordInScript
  f.input :pronunciation, :required => false, :as => :file
end

f.inputs do
  f.has_many :meanings, heading: 'Meanings', allow_destroy: true, new_record: true do |a|
    a.input :meaning
    a.input :language
  end
end

f.actions
end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails has-many-through activeadmin ruby-on-rails-4

4
推荐指数
1
解决办法
1774
查看次数