我想在视图 Rails 中使用模型属性
I wanna use model attribute in view Rails
我正在尝试在模型中使用属性值,但它不起作用..
我有三个型号:
models/resident.rb
class Resident < ActiveRecord::Base
belongs_to :hostel
has_one :user,dependent: :delete
end
models/user.rb
class User < ActiveRecord::Base
belongs_to:resident
end
models/hostel.rb
class Hostel < ActiveRecord::Base
has_many :residents
has_one :rate_card,dependent: :delete
end
架构
居民
create_table "residents", force: :cascade do |t|
t.string "room_number"
t.string "roll_number"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "hostel_id"
end
用户
create_table "users", force: :cascade do |t|
t.string "roll_number"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "resident_id"
end
宿舍
create_table "hostels", force: :cascade do |t|
t.string "hostel"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
现在我想使用users/show中的旅馆属性值。html.erb
我可以做到这一点:
<%if @user.resident.roll_number=="101303110"%>
如果卷号存在则返回 true..
但如果使用 :
<%if @user.resident.hostel=="J"%>
如果 J 是旅舍模型中的旅舍,则它返回 false。
但是当我们把<%@user.resident.hostel%>
放在show.html.erb中时,它显示的是值J。
我应该如何在彼此的视图中使用相关的模型属性?
根据您的联想,@user.resident.hostel
会加载旅馆。但是您想在 hostel
上比较 hostel
字符串。因此你的比较应该是:
<% if @user.resident.hostel.hostel == 'J' %>
解释:
@user # returns your a user
@user.resident # follows `belongs_to :resident` and
# returns a resident
@user.resident.hostel # follows `belongs_to :hostel` on the resident and
# returns a hostel
@user.resident.hostel.hostel # returns the value store in the `hostel`
# column of that `hostel`
顺便说一句。我认为像这样的链接调用违反了 Law of Demeter。但是,如果没有对您的应用程序有更多的了解,就很难提出任何替代方案。
我正在尝试在模型中使用属性值,但它不起作用.. 我有三个型号:
models/resident.rb
class Resident < ActiveRecord::Base
belongs_to :hostel
has_one :user,dependent: :delete
end
models/user.rb
class User < ActiveRecord::Base
belongs_to:resident
end
models/hostel.rb
class Hostel < ActiveRecord::Base
has_many :residents
has_one :rate_card,dependent: :delete
end
架构
居民
create_table "residents", force: :cascade do |t|
t.string "room_number"
t.string "roll_number"
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "hostel_id"
end
用户
create_table "users", force: :cascade do |t|
t.string "roll_number"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "resident_id"
end
宿舍
create_table "hostels", force: :cascade do |t|
t.string "hostel"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
现在我想使用users/show中的旅馆属性值。html.erb 我可以做到这一点:
<%if @user.resident.roll_number=="101303110"%>
如果卷号存在则返回 true.. 但如果使用 :
<%if @user.resident.hostel=="J"%>
如果 J 是旅舍模型中的旅舍,则它返回 false。
但是当我们把<%@user.resident.hostel%>
放在show.html.erb中时,它显示的是值J。
我应该如何在彼此的视图中使用相关的模型属性?
根据您的联想,@user.resident.hostel
会加载旅馆。但是您想在 hostel
上比较 hostel
字符串。因此你的比较应该是:
<% if @user.resident.hostel.hostel == 'J' %>
解释:
@user # returns your a user
@user.resident # follows `belongs_to :resident` and
# returns a resident
@user.resident.hostel # follows `belongs_to :hostel` on the resident and
# returns a hostel
@user.resident.hostel.hostel # returns the value store in the `hostel`
# column of that `hostel`
顺便说一句。我认为像这样的链接调用违反了 Law of Demeter。但是,如果没有对您的应用程序有更多的了解,就很难提出任何替代方案。