rails json api中的过滤关系使用了很多对很多

时间:2017-10-19 19:01:36

标签: ruby-on-rails json

我有这些模特:

public class MainActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;


private ViewPager mViewPager;

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



     // Create the adapter that will return a fragment for each of the three
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    //setup the viewPager adapter
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);



    final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            Intent startIntent = new Intent(getApplicationContext(), BroadcastPage.class);
            //Go To Log In XML File
            startActivity(startIntent);

            Intent welcome = new Intent(MainActivity.this, BroadcastPage.class);


            finish();

        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(menu_main, menu);
    return true;
}



@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {

        // launch settings activity
        startActivity(new Intent(MainActivity.this, SettingsActivity.class));


        return true;
    }

    return super.onOptionsItemSelected(item);
}



  public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {


   case 0:
    World activity_world_tab = new Worldtab();
   return activity_world_tab;


   case 1:
    BroadcastTab activity_broadcast_tab = new BroadcastTab();
   return activity_broadcast_tab;



case 2:
    GroupsTabBar activity_groups_tabbar = new GroupsTabBar();
    return activity_groups_tabbar;


            default:
               return null;

        }

    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 3;
    }



    @Override
    public CharSequence getPageTitle(int position) {



        switch (position) {
            case 0:
                return "";
            case 1:
                return "SECTION 2";
            case 2:
                return "SECTION 3";
        }
        return null;

        }
      }
      }

class Account < ApplicationRecord
  has_many :account_members, dependent: :destroy
  has_many :users, through: :account_members
  has_many :projects, dependent: :destroy

end

class Project < ApplicationRecord
  belongs_to :account
  has_many :project_members, dependent: :destroy
end

class ProjectMember < ApplicationRecord
  belongs_to :project
  belongs_to :account_member
end

我的帐户的控制器索引功能

class AccountMember < ApplicationRecord
  belongs_to :account
  belongs_to :user
  has_many :project_members, dependent: :destroy
end

问题是此操作返回包含所有项目的帐户,但用户可能只是2个项目的成员。我希望它只返回用户可以通过project_members关系访问的项目。

你是怎么做到的?

编辑1 如果有所作为,我也在使用cancancan。

编辑2 这是帐户序列化程序

  def index
    @accounts = @current_user.accounts

    render json: @accounts, include: params[:include]
  end

1 个答案:

答案 0 :(得分:2)

当您使用cancancan时,您可以通过能力过滤它 在AccountSerializer

has_many :projects do
  ability = Ability.new(scope)
  @object.projects.select{ |p| ability.can?(:read, p) }
end