首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rails系统关联工作半程

rails系统关联工作半程
EN

Stack Overflow用户
提问于 2014-05-20 14:11:57
回答 3查看 35关注 0票数 0

我做了一个简单的联想--一个丰富的联合餐桌--通过参与向用户提供宴会,并通过参与为用户提供盛宴。但只有从侧面看,它才是完全合情合理的:

user.rb:

代码语言:javascript
复制
class User < ActiveRecord::Base

  require 'digest/sha1'
  mount_uploader :image, ImageUploader

  has_many :participations
  has_many :feasts, :through => :participations

participation.rb:

代码语言:javascript
复制
class Participation < ActiveRecord::Base

  belongs_to :user
  belongs_to :feast

  has_many :obligations
  has_many :dishes, :through=> :obligations
  has_many :groceries, :as => :needed 

end

feast.rb:

代码语言:javascript
复制
class Feast < ActiveRecord::Base

  mount_uploader :image, ImageUploader

  has_many :participations
  has_many :users, :through => :participations

  has_many :courses
  has_many :dishes, :through=>:course

  has_many :groceries, :as => :needed  

  has_many :feast_invt, :as => :invitable

  validates_presence_of :feast_time
  validates_presence_of :feast_place


end

加载相关数据库:

代码语言:javascript
复制
irb(main):001:0> feast = Feast.find(1)

  ←[1m←[36mFeast Load (1.0ms)←[0m  ←[1mSELECT `feasts`.* FROM `feasts` WHERE `fe
asts`.`id` = ? LIMIT 1←[0m  [["id", 1]]

=> #<Feast id: 1, cost: nil, feast_place: "home", feast_time: "2014-03-14 00:00:
00", created_at: "2014-05-19 17:50:30", updated_at: "2014-05-19 17:50:30", image
: nil>

irb(main):002:0> user=User.find(3)

  ←[1m←[35mUser Load (1.0ms)←[0m  SELECT `users`.* FROM `users` WHERE `users`.`i
d` = ? LIMIT 1  [["id", 3]]

=> #<User id: 3, name: "elad bezalel", password: "", email: "
", hashed_password: "", creat
ed_at: "2014-05-14 18:30:46", updated_at: "2014-05-14 18:30:46", shop_cost: nil,
 salt: "", city: "", street_num
: "", entrance: "b", level: "3", apartment_num: "7", neighborhood: "
", kosher?: nil, image: "el4.jpg">
irb(main):003:0> par = Participation.find(1)

[1][1M][36 36mParticipation (1.0ms)←[0m←][1mSELECT participations.* FROM p articipations participations.id =?限制1←[0m ["id",1]

代码语言:javascript
复制
=> #<Participation id: 1, user_id: 3, feast_id: 1, created_at: "2014-05-19 17:54
:30", updated_at: "2014-05-19 17:54:30", user_costs: nil, meneger?: nil, accepte
d?: nil, coming???: nil>

irb(main):004:0>

问题是当我写到:

代码语言:javascript
复制
users.feasts 

正常工作

但相反的情况却不起作用:(尽管这完全是一种恭维!)

代码语言:javascript
复制
irb(main):002:0> feast.users


NoMethodError: undefined method `users' for #<ActiveRecord::Relation::ActiveReco
rd_Relation_Feast:0x548cfa0>


  from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4
 .0.2/lib/active_record/relation/delegation.rb:121:in `method_missing'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-4
 .0.2/lib/active_record/relation/delegation.rb:68:in `method_missing'
    from (irb):2
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
 /lib/rails/commands/console.rb:90:in `start'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
 /lib/rails/commands/console.rb:9:in `start'
    from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-4.0.2
  /lib/rails/commands.rb:62:in `<top (required)>'
    from bin/rails:4:in `require'
    from bin/rails:4:in `<main>'

我的相关schema.rb:

代码语言:javascript
复制
create_table "feasts", force: true do |t|
    t.integer  "cost"
    t.string   "feast_place"
    t.datetime "feast_time"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "image"
 end

create_table "participations", force: true do |t|
    t.integer  "user_id"
    t.integer  "feast_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.integer  "user_costs"
    t.boolean  "meneger?"
    t.boolean  "accepted?"
    t.string   "coming???"
  end

  add_index "participations", ["user_id", "feast_id"], name:          
             index_participations_on_user_id_and_feast_id", using: :btree

  create_table "users", force: true do |t|
     t.string   "name",            limit: 75
     t.string   "password",        limit: 40
     t.string   "email",                      default: "", null: false
     t.string   "hashed_password"
     t.datetime "created_at"
     t.datetime "updated_at"
     t.integer  "shop_cost"
     t.string   "salt"
     t.string   "city"
     t.string   "street_num"
     t.string   "entrance"
     t.string   "level"
     t.string   "apartment_num"
     t.string   "neighborhood"
     t.string   "kosher?"
     t.string   "image"
  end

end

我甚至试图添加另一个索引

代码语言:javascript
复制
  "participations", ["feast_id", "user_id"]

因此,这也将是系统的,但它不起作用。

该怎么办??

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-20 14:16:00

您的feast变量实际上是ActiveRecord::Relation,而不是Feast的单个实例。您只能在Feast的实际实例上调用users

如何加载feast变量?

票数 1
EN

Stack Overflow用户

发布于 2014-05-20 14:26:38

我认为你的问题只是你没有正确地定义变量。

这会很好的

代码语言:javascript
复制
user = User.find(1)
user.feasts
feast = Feast.find(1)
feast.users

你有以下几点:

代码语言:javascript
复制
users.feasts 

rails抱怨您没有定义users。这是真的,你没有。

你是不是犯了一个错误?

代码语言:javascript
复制
feast = Feast.first
feast.users

这定义了一个局部变量users?因为它没有。

你可以

代码语言:javascript
复制
feast = Feast.first
users = feast.users
#=> a collection of users

代码语言:javascript
复制
feast = Feast.first
user = feast.users.first
user.feasts
#=> get a single user and call .feasts on it
票数 1
EN

Stack Overflow用户

发布于 2014-05-21 15:01:45

我在==>上面的问题中也写过

代码语言:javascript
复制
feast = feast.find(3)

当问题提出时,这并不是我真正写的。我真正做的是==>

代码语言:javascript
复制
feast = Feast.all 

因为我在数据库中只有一张盛宴记录,所以我不认为这是一个问题(所以我甚至没有在问题中提到它,并写了另一行作为示例)。

使宴会成为一个积极的关系对象。所以现在问题已经解决了,谢谢你的快速回答。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23762184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档