Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

associations - Rails - how to show attribute of an associated model

I am trying to make an app in Rails 4.

I just asked this related question and got a clear answer. It seems I can't understand how to take that logic and apply it elsewhere.

Rails How to show attributes from a parent object

I have a user model, profile model a projects model and a universities model.

Associations are:

Profile belongs to university
Profile belongs to user
University has many profiles
University has many projects
Projects HABTM user
Projects belong to universities

In my projects controller, I define @creator as follows:

def create
    logger.debug "xxx create project"
    #authorise @project
    @project = Project.new(project_params)
    @project.creator_id = current_user.id
    @project.users << current_user
    respond_to do |format|
      if @project.save

        format.html { redirect_to @project }
        format.json { render action: 'show', status: :created, location: @project }
      else
        format.html { render action: 'new' }
        format.json { render json: @project.errors, status: :unprocessable_entity }
      end
    end
  end

I try to define creator_profile like this:

def show
    #authorise @project

    @project = Project.find(params[:id])
    @creator = User.find(@project.creator_id)
    @creator_profile = @creator.profile

  end

In my uni table, I have attributes called logo and name. I use avatar uploader in which i have logo defined (that's why I have two .logo below).

In my projects, show, I want to display the university that the project creator belongs to.

I've tried this:

<%= image_tag(@creator_profile.university.logo.logo) %> 
        <div class="generaltext"><%= @creator_profile.university.name %> </div>

I get this result: undefined method `logo' for nil:NilClass

Based on the link to my problem above

<%= image_tag(creator_profile.university.logo.logo) %> 
            <div class="generaltext"><%= creator_profile.university.name %> </div>

I get this result:

undefined local variable or method `creator_profile' for #<#<Class:0x007f998f17ad88>:0x007f998d1ce318>

I'm not sure I understood the very detailed explanations given in the answer to my previous question. If the first version is right, then I don't understand the explanation at all. If the second version is right, then why does this error message come up?

Im wondering if the problem arises out of there not being an association between university and user? I was hoping, based on the user who created the project, to find the uni that the creator belongs to.

That's why i tried:

<%= image_tag(creator_profile.project.university.logo.logo) %> 
                <div class="generaltext"><%= creator_profile.project.university.name %> </div>

I get this error:

undefined method `project' for #<Profile:0x007f998ada41b8>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think that you need to understand some basic concepts of Ruby and Ruby and Rails to solve this question yourself.

In ruby, vars with @ are instance variables and are available all over the class. That means that they will be available in your view if you declare them in your controller.

EG @creator_profile = @profile.user

On the other hand, vars without @ are only available inside the same block.

An example:

#controller
@users = User.all #@users, instance variable

#view
<% @users.each do |user| %>
  <h3><%= user.name  %></h3> #user, local variable. This will work
<% end %>
<h3><%= user.name %></h3> #this won't work because it is outside the block

Google about ruby vars and scopes.

Also, I think that you are relying too much on 'rails magic' (or you are skipping some code lines), if you don't declare an instance var, it won't exist. Naming conventions don't work that way.

At last but not at least, having a look at your relations, I think that they need some refactor. Also the use of singular and plural is not correct. I know that it's not real code but it denotes that they don't reflect real relationships between entities.

Don't try to make 'octopus' models, where everybody belongs to everybody, and think about the relationships itself, not only trying to associate models. EG:

Profile
belongs_to :creator, class_name: 'User'

This way you can write:

#controller
@profile_creator = Profile.find(params[:id]).creator

#view
@profile_creator.university

You will understand better what you are doing.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...