Я не понимаю, как получить доступ к данным из моего просмотра в другой таблице.
У меня мало моделей.Первый - Пользователь , второй - UsersPreferences .Итак, у меня есть страница настроек для пользователей, и код выглядит так:
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
...
<div class="checkbox">
<%= f.check_box :notification_about_new_followers %>
<%= f.label 'Notify via email about new followers' %><br />
</div>
Этот код работает ( Пользователь имеет строку messages_about_new_followers ), но я хочу изменить его.Я решил, что лучше всего сохранить предпочтения в другой таблице.Итак, я создал модель UsersPreferences , которая имеет user_id и флаг ' notify_about_new_followers '.Я пытался переписать мой взгляд так:
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
...
<div class="checkbox">
<%= f.check_box UsersPreferences.find_by_user_id(@user.id).notify_about_new_followers %>
<%= f.label 'Notify via email about new followers' %><br />
</div>
Но когда я захожу на страницу с кодом, я получаю такую ошибку
undefined method `true' for #<User:0x007f8ac180dee8>
Как я могу решить эту ошибку?
Пользователь модель:
class User < ActiveRecord::Base
attr_accessible :name, ... , :notification_about_new_followers
...
has_one :users_preferences
end
UsersPreferences модель:
class UsersPreferences < ActiveRecord::Base
belongs_to :user, :class_name => "User"
attr_accessible :notify_about_new_followers
end