1

I have a Developers Table and a Projects Table.I am trying to assign a one to many relationship between these two tables in my rails application. Were I will add developers to projects.

developer belongs to a project ,project has many developers

so I did the following.

I created this method :

 def add_to_project


@project = Project.find_by(:title => params[:title])
@developer = Developer.find_by(:name => params[:name])

unless @developer.nil? ||  @project.nil?

  # @project.developers.push(@developer)
  @project.developers << @developer

  if @project.save

    flash[:notice] = "project was added successfully"
  else
    flash[:notice] = "project was not added successfully"
  end

else
   flash[:notice] = "project was not added successfully"

end

 redirect_to(:action => 'add_dev_proj') # this goes to a page that shows the two tables with the updates
end

I obtain the values of the title and name from a form_tag I constructed like the following:

<%= form_tag(:controller => 'projects', :action => 'add_to_project') do %>
<%= text_field_tag('name', params[:name]) %>
<%= text_field_tag('title', params[:title]) %>

<%= submit_tag("Add dev to project") %>
<% end %>

My problem is when trying this approach using rails console it works, but in my web app no modifications are being made. is there something I am missing here. Also I made sure my associations are defined in my model.

Any insights would be great, thanks a lot in advance.

Ab Mu
  • 13
  • 4
  • why don't you use has_many relationhip, you don't have to use `append` then – Sonalkumar sute Feb 11 '15 at 10:44
  • Has many is used, 'has_many :developers' , but the issue is when I want to add developers to my project this is done using the append '<<' operator. – Ab Mu Feb 11 '15 at 10:52
  • add `belongs_to :project` in developer model and `has_many: developers` in project model and have you added foreign_key in developers table as `project_id` – Sonalkumar sute Feb 11 '15 at 10:57
  • I already had this implemented, the append operation works in the rails console when I tested it. But in my web app it doesn't. – Ab Mu Feb 11 '15 at 11:14
  • If all implemented then `@project.developers` this line should give you all the developers for particular project. If still you want to use append Check what's this it's a relation object or array `@project.developers` – Sonalkumar sute Feb 11 '15 at 11:18
  • At the dev_to_project view page I try to render @project.developers.size it shows value 0 which mean the deveopler wasn't added. – Ab Mu Feb 11 '15 at 11:21
  • from where you are adding project and developers from console, if so check that data is getting saved properly to db. Use `save!` method – Sonalkumar sute Feb 11 '15 at 11:23
  • I used the save method, also I can add from forms in my web app. This is not the case what I meant is that I tested this feature on rails console before implementing it and it is working. But it doesn't seem to work in my rails app. – Ab Mu Feb 11 '15 at 11:36

2 Answers2

0

In Developer model

belongs_to :project

In project model

has_many: developers 

add foreign_key in developers table as project_id

Then you can directly call

@project = Project.find_by(title:  params[:title])
@project.developers.create(firstname: params[:name])
Sonalkumar sute
  • 2,565
  • 2
  • 17
  • 27
0

A missing detail I forgot to mention is that my developer table has a password, and I have password authentication for it enabled, causing a failure to save whenever I called developer.save. now I provided developer.password as an experiment (I was 90% sure it is gonna work), and then the save was successful "my data base was updated".

When I attempted to save after appending

irb(main):030:0> dev.save
 (0.1ms)  BEGIN
 Developer Exists (0.2ms)  SELECT  1 AS one FROM `developers` WHERE    (`developers`.`email` = BINARY 'dev1@nc.com' AND `developers`.`id` != 1) LIMIT 1
(0.1ms)  ROLLBACK
=> false

The error message I got:

irb(main):031:0> dev.errors.full_messages
=> ["Password can't be blank", "Password is too short (minimum is 4 characters)"]

So my next step is to find a way to let my app know that I am an admin and don't need to authenticate the password in order to do successful save.

Thanks a lot for you time and suggestions ...cheers :-).

Ab Mu
  • 13
  • 4