Как я могу исправить эту модель ассоциации? - PullRequest
0 голосов
/ 20 июля 2011

Я создал модель альбома и фотомодель и добавил ее в существующее приложение rails. Я сделал модель фотографий принадлежащей модели альбома, а модель альбома - существующей модели профиля, которая принадлежит модели пользователя. Я не знаю, связал ли я их неправильно и почему я получаю ошибку.

Стоит отметить, что когда я захожу на URL / альбомы, тогда все работает как надо, но когда я захожу на URL / profile / 1 (приведенный ниже код вставляется в файл show.html.erb в views / profile / папку), то я получаю ошибку ниже.

Это простая проблема, которую я просто не могу решить. Ниже приведены четыре файла модели:

Album.rb:

class Album < ActiveRecord::Base
  belongs_to :profile
  has_many :photos, :dependent => :destroy
  accepts_nested_attributes_for :photos, :allow_destroy => true
end

Profile.rb:

class Profile < ActiveRecord::Base

belongs_to :user
has_many :albums

def self.get_location(profile)
  location = []

  location << profile.city unless profile.city.blank?

  location << profile.state unless profile.state.blank?

  location << profile.country unless profile.country.blank?

  location << profile.postal_code unless profile.postal_code.blank?

  location
end

def self.missing_fields(profile)
  missing = []

  if profile.first_name.blank?
    missing << "first name"
  end

  if profile.last_name.blank?
    missing << "last name"
  end

  if profile.job_title.blank?
    missing << "job title"
  end

  missing
end

end

Photo.rb:

require 'paperclip'
class Photo < ActiveRecord::Base

  belongs_to :album
  has_attached_file :upload,
                :url => "/images/:id/:style/:basename.:extension",
                :path => ":rails_root/public/images/:id/:style/:basename.:extension",
                :styles => {
                  :thumb => "75x75>",
                  :small => "200x200>"
                }
  #add in any validations you may want
end

User.rb:

class User < ActiveRecord::Base

  include Gravtastic
  gravtastic :size => 120

  # associations
  has_many :albums
  has_many :photos, :through => :albums
  has_many :authorizations, :dependent => :destroy
  has_one :profile, :dependent => :destroy
  has_many :resumes, :dependent => :destroy, :order => 'created_at DESC'
  has_many :thoughts, :dependent => :destroy, :order => 'created_at DESC'
  has_many :user_threads, :dependent => :destroy, :order => 'created_at ASC'

  accepts_nested_attributes_for :profile

  # virtual attributes
  attr_accessor :first_name, :last_name

  # validations
  validates_presence_of :first_name
  validates_presence_of :last_name
  validates_length_of :username, :minimum => 4, :message => " is too short"
  validates :email, :email => {:message => " is not valid"}
  validates_uniqueness_of :email, :case_sensitive => false
  validates_uniqueness_of :username, :case_sensitive => false
  validates_length_of :password, :minimum => 4, :message => " is too short"

  # authlogic
  acts_as_authentic do |config|
  config.crypto_provider = Authlogic::CryptoProviders::MD5
  config.maintain_sessions = false
  config.validate_email_field = false
  config.validate_login_field = false
  config.validate_password_field = false
  config.login_field = :email
  config.validate_login_field = false
end

  def self.create_from_hash!(hash)
    user = User.new(:username => Time.now.to_i, :email => '', :auth_provider => hash['provider'])
    user.save(:validate => false)
    if hash['provider'].downcase == 'twitter'
    user.profile = Profile.create(:first_name => Twitter::Client.new.user(hash['user_info']    ['nickname'].to_s).name)
else
  user.profile = Profile.create(:first_name => hash['user_info']['first_name'], :last_name => hash['user_info']['last_name'])
end
user
end

  def deliver_password_reset_instructions!
    reset_perishable_token!
    UserMailer.deliver_password_reset_instructions(self)
  end

  def activate!
   self.active = true
   save(false)
  end

  def deliver_activation_instructions!
   reset_perishable_token!
   UserMailer.deliver_activation_instructions(self)
  end

end

Контроллер профиля имеет этот фрагмент:

  def show
    @user = User.find_by_username(params[:id])
    @profile = @user.profile

    @location = Profile.get_location(@profile)

    @resumes = @user.resumes

    @albums = @user.albums

    @photos = @user.photos

    @thoughts = @user.thoughts

    @shouts = UserThread.find_profile_shouts(@profile)

    @shouters = UserThread.find_shouters(@shouts)

    @user_thread = UserThread.new
  end

Вид имеет это:

<div id="profile_right_col">
    <h2>Albums</h2>
    <p>
      <b>Name:</b>
      <%= @albums %><br />

      <% @albums.photos.each do |photo| %>
        <h3><%= photo.title %></h3>
        <%= image_tag photos.upload.url(:small) %>
      <% end %>
    </p>

    <%= link_to 'Edit', edit_album_path(@albums) %> |
    <%= link_to 'Back', albums_path %>
</div>

Исключение Action Controller показывает:

ActiveRecord::StatementInvalid in Profiles#show

Showing /Users/pawel/Ruby/Apps/cvf/app/views/profiles/show.html.erb where line #137 raised:

SQLite3::SQLException: no such column: albums.user_id: SELECT     "albums".* FROM       "albums"  WHERE     ("albums".user_id = 4)

Extracted source (around line #137):

 <h2>Albums</h2>
<p>
<b>Name:</b>
<%= @albums %><br />

<% @albums.photos.each do |photo| %>
<h3><%= photo.title %></h3>

Ответы [ 2 ]

0 голосов
/ 04 августа 2011

Это сработало после того, как я добавил Paperclip::Railtie.insert в мое application.rb.

0 голосов
/ 20 июля 2011

У вас нет переменной @album, определенной в вашем действии show (у вас есть @albums и в представлениях, которые вам нужно пройти через массив @albums).Таким образом, его значение равно нулю, и у него нет фотографий метода.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...